PHP Include:
Without understanding much about the details of PHP, you can save yourself a great deal of time with the use of the PHP include command. include takes a file name and simply inserts that file's contents into the script that issued the include command.
An Include Example
Say we wanted to create a common menu file that all our pages will use. A common practice for naming files that are to be included is to use the ".php" extension. Since we want to create a common menu let's save it as "menu.php".menu.php Code:
<html> <body> <a href="http://www.example.com/index.php">Home</a> - <a href="http://www.example.com/about.php">About Us</a> - <a href="http://www.example.com/links.php">Links</a> - <a href="http://www.example.com/contact.php">Contact Us</a> <br />
index.php Code:
<?php include("menu.php"); ?> <p>This is my home page that uses a common menu to save me time when I add new pages to my website!</p> </body> </html>
Display:
Home -
About Us -
Links -
Contact Us This is my home page that uses a common menu to save me time when I add new pages to my website!
And we would do the same thing for "about.php", "links.php", and "contact.php". Just think
how terrible it would be if you had 15 or more pages with a common menu and you decided to add
another web page to that site. You would have to go in and manually edit every single file to add this
new page, but with include files you simply have to change "menu.php" and all your problems are solved.
Avoid such troublesome occasions with a simple include file.What do Visitors See?
If we were to use the include command to insert a menu on each of our web pages, what would the visitor see if they viewed the source of "index.php"? Well, because the include command is pretty much the same as copying and pasting, the visitors would see:View Source of index.php to a Visitor:
<html> <body> <a href="index.php">Home</a> - <a href="about.php">About Us</a> - <a href="links.php">Links</a> - <a href="contact.php">Contact Us</a> <br /> <p>This is my home page that uses a common menu to save me time when I add new pages to my website!</p> </body> </html>
Include Recap
The include command simply takes all the text that exists in the specified file and copies it into the file that uses the include command. Include is quite useful when you want to include the same PHP, HTML, or text segment on multiple pages of a website. The include command is used widely by PHP web developers. Like PHP Echo, include is not a function, but a language construct.The next lesson will talk about a slight variation of the include command: require. It is often best to use the require command instead of the include command in your PHP Code. Read the next lesson to find out why!
______________________________________________________________________________
PHP Require
Just like the previous lesson, the require command is used to include a file into your PHP code. However there is one huge difference between the two commands, though it might not seem that big of a deal.Require vs Include
When you include a file with the include command and PHP cannot find it you will see an error message like the following:PHP Code:
<?php include("noFileExistsHere.php"); echo "Hello World!"; ?>
Display:
Warning: main(noFileExistsHere.php): failed to open stream: No such file or directory in /home/websiteName/FolderName/tizagScript.php on line 2
Warning: main(): Failed opening 'noFileExistsHere.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/websiteName/FolderName/tizagScript.php on line 2
Hello World!
Notice that our echo statement is still executed, this is because a Warning does not prevent our PHP script from running.
On the other hand, if we did the same example but used the require statement we would get something like the following example.Hello World!
PHP Code:
<?php require("noFileExistsHere.php"); echo "Hello World!"; ?>
Display:
Warning: main(noFileExistsHere.php): failed to open stream: No such file or directory in /home/websiteName/FolderName/tizagScript.php on line 2
Fatal error: main(): Failed opening required 'noFileExistsHere.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/websiteName/FolderName/tizagScript.php on line 2
The echo statement was not executed because our script execution died after the require
command returned a fatal error! We recommend that you use require
instead of include because your scripts should not be executing if
necessary files are missing or misnamed.Fatal error: main(): Failed opening required 'noFileExistsHere.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/websiteName/FolderName/tizagScript.php on line 2
1 comments:
Nice difference between php include and file include.
PHP Development Company
Post a Comment