Jump to content

Similar functions, which are best?


Prodigal Son

Recommended Posts

Hi,

 

I'm new to PHP and I noticed there are a lot of similar functions. I was wondering if anyone knows of a list of functions that should almost always be used over the other whether it is because one is more secure, faster, etc.? I.e. die vs exit, print vs echo (well I guess everyone uses echo instead), include vs include_once (is there any harm in always using include_once?), etc. etc. Often enough I will hear someone saying never use this function, always use this function instead, or something similar. Hopefully such a list of comparison exists.

 

Also, does anyone have any good sources for topics such as best coding practices for php, or better ways to design your page? For instance, if you have a form, should everything be on one page? That is, check if the submit button is set and then process the form if it does not exist and if the submit button is set you process the results. Or should you be using two pages so that your form processes to another page?

 

Or any kind of information like that.

 

I figure if I'm trying to learn the language, I mind as well make some good habits as I begin.

 

Thanks.

Link to comment
Share on other sites

As far as the functions go, I wouldn't say there are any functions you should never use. There are some functions that are identical (like your print/echo and die/exit example), but those cases are minimal and usually only exist for backwards compatibility.

 

You're other example of include though needs some explanation. There are 4 'include' functions: include, include_once, require, and require_once. I would recommend on giving their descriptions a read through on the PHP site, because while they all generally do the same thing, they have very distinct uses.

Link to comment
Share on other sites

Oh ya, the require will stop executing the script if it cannot find the include right? Whereas the include will continue to process the script even if it cannot find it? But is there any harm in using include_once/require_once instead of include/require? (Actually I saw a google book that suggested to always use include_once instead of include as there is no harm in doing so) I couldn't think of an example where you would want the possibility of including more than once. Or maybe include_once/require_once are slower? But since I still a lot of people using include instead I am wondering.

Link to comment
Share on other sites

Well I don't know much but when your including your database conenction file or something very important with important information inside it you should always use the require_once "dbconnection.php"; rule instead of include.

 

Also when using print and echo, i would say its better to use echo because its obviously 1 char shorter ;) and its easy to understand. Both however are used to output strings in the same way.

 

In terms of page design I would say that for example, use include tags wherever you can. For example use what you would call a master slide like in ms powerpoint. Example:

 

[Logo here]
[menu]     [Table here. This is where all the main comments go]     [menu2]
[copyright stuff etc]

 

Okay so basically you would put the following into a file called header.php ....

 

[Logo here]
[menu]     

 

and then with menu 2 put that in a file called footer.php ...

 

[menu2]

 

And then everytime you make a page.... You can just do this...

 

randompage.php

<? include "header.php"; ?>
All your main content here
<? } ?>

 

Heres an example page i set up quickly to show how this can work...

 

http://www.ragingmortals.com/game/froob.php

sorry you have to login to view it. user: demo    pass: demo

 

code for that page:

<? include "*******/header.php"; ?>
Hello you!
<? include "*******/footer.php"; ?>

 

Its very good for making sites. Also you can take the friendly ajax route to making sites although that'll take quite some time. Anyway hope this helped you.

Link to comment
Share on other sites

Oh ya, the require will stop executing the script if it cannot find the include right? Whereas the include will continue to process the script even if it cannot find it? But is there any harm in using include_once/require_once instead of include/require? (Actually I saw a google book that suggested to always use include_once instead of include as there is no harm in doing so) I couldn't think of an example where you would want the possibility of including more than once. Or maybe include_once/require_once are slower? But since I still a lot of people using include instead I am wondering.

 

Well, I recommended you do some reading, but clearly you didn't want to do that. Speed really isn't an issue when it comes to the four include functions because they are all different. So, using one over the other isn't an question of which is faster, but rather which is the right one.

 

include vs require - include tries to include the file, no questions asked. require will halt the script if it can't find the file.

*_once - With the once versions of include/require, the statement will be skipped if the file has already been included somewhere else. a good example is a file that does your database connection. while several different files may include this db file, you only want that db code run once. So example:

 

<?php
  //This file does all the database connection stuff
?>

<?php
  require_once('dbconnect.php');
  //This file builds a table of info from the db
?>

<?php
  require_once('dbconnect.php');
  //This file also builds a table of different info from the db
?>

<?php
  //This is the actual script that loads in the browser
  print "Here is table 1:";
  include('table1.php'); //Include the code for table1
  print "Here is table 2:";
  include('table2.php'); //Include the code for table2
?>

When table 2 is included, you don't want it to re-establish a connection to the database, because you already have one. By using require_once() on the db file, it will skip the once in table2.php because it was already included with table1.php.

Link to comment
Share on other sites

Yah, I understand that there are differences between the include and require and using the right one for each scenario. But what I was asking was include vs include_once and require vs require_once. As in, is there any harm in always using the _once instead. In your example, instead of using just include, what if I use include_once? In other words, just to not potentially confuse myself, wouldn't it be a better idea to just limit myself to using only include_once and require_once?

Link to comment
Share on other sites

depends on what you are trying to do. in my example i would not use include_once() instead of include because include_once() would block any future tries to include table1.php. i mean, if you are writing the code all yourself, and you know what everything is doing, and you know it will never get included again, there is no harm in using include_once(). but, when you are collaborating, and several people are working on one project, you would want to use what is logical. and since there is no reason table1.php couldn't be included again somewhere else in the script, you should probably use include instead of include_once.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.