PDA

View Full Version : Is there a PHP version of the ASP #include?



dog
10-10-2007, 08:23 PM
Hello people,

A quick question: is there a PHP version of the ASP #include?

I'm talking about a command that brings script onto the page from an external file.

Great for elements like menus that appear on various pages because when you need to edit them you just edit the external file.

Thanks for any help,

Dog

boogyman
10-10-2007, 08:25 PM
<?php include_once('/path/to/file.ext') ?>


that will spit out a warning but will continue to process the rest of the code... if the file you are attempting to bring in is critical you can use



<?php require_once('/path/to/file.ext') ?>

dog
10-10-2007, 08:28 PM
Thanks for the quick reply!




<?php include_once('/path/to/file.ext') ?>


that will spit out a warning but will continue to process the rest of the code... if the file you are attempting to bring in is critical you can use



<?php require_once('/path/to/file.ext') ?>


What is the effect of spitting out a warning? Sounds bad :confused:

What do you mean by critical. Sounds serious! :confused:

Thanks

boogyman
10-10-2007, 08:45 PM
think of this situation...
you are ddamin (owner of this site), and rebuilding a new login system.
when you want the userCP interface to look totally different, so you create a new process page.
connecting to the database is a critical step.. if you cannot select the user, well then it doesnt matter if you can or cannot do something else.... thus thats critical, and really nothing else should be done if you cannot connect to the database... thus you should require that file.

okay so the user is in and inside their control panel page. the user now would like to print the page for personal use, but the css print styles are not prepared correctly... this would be a situation where you could use an include, because you would like the print styles to not print all of the extra colors n background images, but if it does well its not the end of the world.

basically if something must be used you should make it required becaues it will terminate the script and give an error
if something should be included but is not absolutely necessary you should use include which will just give a warning.

both of these will be parsed in the error log located on the local machine, and unless handled differently, they will print the error / warning on the page

djr33
10-10-2007, 10:49 PM
The error will only be generated if the page DOESN'T exist.

you could also use:
<?php $f='my/path/myfile.ext'; if (file_exists($f)) { include($f); } ?>
That will check that it exists and if so, output.

Twey
10-11-2007, 02:37 AM
But that's silly, because this check is built into the include keyword, so you're effectively checking twice. You might as well just do:
<?php @include 'my/path/myfile.ext'; ?>

djr33
10-11-2007, 05:00 AM
Hm. I didn't do that because I thought there were some strange error suppression restrictions on include, though I must have imagined that.
It does seem that will work fine, though the if format would at least give an alternate option.

Twey
10-11-2007, 05:25 AM
Not as far as I'm aware.
the if format would at least give an alternate option.That's true. You could also calculate the precise distance between the Earth and Jupiter, then ignore the result before using the include statement. :)

djr33
10-11-2007, 06:18 AM
500,598,2..... nevermind.

With an include, though, it makes sense to have some backup if it failed, though in this case, it may not be dynamic anyway.

I looked on PHP.net for comments on the subject, and I don't see anything like that, so I must have imagined it, but I thought that I read somewhere that error suppression causes problems with certain functions and that include was a "big enough", "construct [not function]" that it fell into this.
Again, though, probably just thinking of something else.