PHP Include/Require Statements

Include and require statements can be very useful in PHP and website design. They allow PHP coding that was previously written be implemented into the PHP code of the application in question. The code in basically substituted in place of the include or require statement. Once it is substituted the code is run.

They can allow variables from a different page be used in a different page or it can help keep a consistent layout.

Basic syntax is shown below:

include/require 'filename';
				

The difference between include and require is that include will allow the script to continue if it isn't fulfilled while require will halt the entire script in its tracks.

A practical usage of this is for website menues. After a certain point there are so many pages in a website that it becomes a pain to update every single one's menu in an attempt to keep everything up-to-date. The include function gets rid of that!

For example, you could make a base file for a menu like this called "menu.php" (remove the spaces before/after brackets):

echo '< a href="shodor.org" >Shodor Homepage< /a >
< a href="google.com" >Google< /a >
< a href="wikipedia.org" >Wikipedia< /a >
< a href="dictionary.reference.com" >Dictionary< /a >'
				

Then you could put it into your website like this (again, without spaces before/after brackets):

< html >
< body >
< ?php include 'menu.php'; ? >
< /body>
< /html >
				

This will put the menu in place of the PHP code.

You can also use the include_once or require_once to have the code that you want in your script only once, checking if the code has already been implemented then to not do so again. This could be used in cases such as a while or a for loop where the code could be included multiple times and change the end result or the actions of the function.

Obviously this has many different uses for the user to decide. You can use it to keep things clean and organized, or you can use it to transmit data to multiple sources instead of having to retype it several times.