php – reference

Posted by Jiltin     7 July, 2009    439 views   

This Post will be continuously updated with comments, help text about php and the usage:

Question: What the difference between include() and include_once() was. php.net says include_once only includes a file once. Does this mean if I use include() I can access variables from the included file anywhere in my script?

Answer:include_once() will not include a file that has already been included. include() will include a file every time. include_once() is useful if you have php scripts which include scripts which include scripts. If 2 of the scripts include the same file then you might get variables and other things being overwritten. Using include_once() ensures you get the functionality that the scripts need but without any side effects.

For example, I have two files, once called functions.php and another is header.php. I include functions.php, and another file called globals.php and I also include functions.php, if I do the include(), my php script will error out. I will get an error saying one of my functions has already been used, why? because I included the file functions.php twice, once in the header and again in globals.php. so to avoid getting an error, it would just be safe to use include_once() instead of include() function.

Following Google Searches Lead To This Post:
how to reference results in php

Following Google Searches Lead To This Post: package pdksh is not installed, Fedora 11

Post to Twitter  Post to Delicious  Post to Digg    Post to StumbleUpon

Categories : Web & Scripts Tags :

Comments

No comments yet.


Leave a comment

(required)

(required)