PHP include and require Statements

Sharing is caring!

It is possible to insert the content of one PHP file into another PHP file (before the server executes it), with the include or require statement.

The include and require statements are identical, except upon failure:

  • require will produce a fatal error (E_COMPILE_ERROR) and stop the script
  • include will only produce a warning (E_WARNING) and the script will continue

So, if you want the execution to go on and show users the output, even if the include file is missing, use the include statement. Otherwise, in case of FrameWork, CMS, or a complex PHP application coding, always use the require statement to include a key file to the flow of execution. This will help avoid compromising your application’s security and integrity, just in-case one key file is accidentally missing.

Including files saves a lot of work. This means that you can create a standard header, footer, or menu file for all your web pages. Then, when the header needs to be updated, you can only update the header include file.

The include() Function

The include() function takes all the text in a specified file and copies it into the file that uses the include function. If there is any problem in loading a file then the include() function generates a warning but the script will continue execution.

Assume you want to create a common menu for your website. Then create a file menu.php with the following content.

<a href="http://www.tutorialspoint.com/index.htm">Home</a> - 
<a href="http://www.tutorialspoint.com/ebxml">ebXML</a> - 
<a href="http://www.tutorialspoint.com/ajax">AJAX</a> - 
<a href="http://www.tutorialspoint.com/perl">PERL</a> <br />

Now create as many pages as you like and include this file to create header. For example now your test.php file can have following content.

<html>
   <body>
   
      <?php include("menu.php"); ?>
      <p>This is an example to show how to include PHP file!</p>
      
   </body>
</html>

The require() Function

The require() function takes all the text in a specified file and copies it into the file that uses the include function. If there is any problem in loading a file then the require() function generates a fatal error and halt the execution of the script.

So there is no difference in require() and include() except they handle error conditions. It is recommended to use the require() function instead of include(), because scripts should not continue executing if files are missing or misnamed.

You can try using above example with require() function and it will generate same result. But if you will try following two examples where file does not exist then you will get different results.

<html>
   <body>
   
      <?php include("xxmenu.php"); ?>
      <p>This is an example to show how to include wrong PHP file!</p>
      
   </body>
</html>

This will produce the following result −

This is an example to show how to include wrong PHP file!

Now lets try same example with require() function.

<html>
   <body>
       
       <?php require("xxmenu.php"); ?>
       <p>This is an example to show how to include wrong PHP file!</p>
   
   </body>
</html>

This time file execution halts and nothing is displayed.

NOTE − You may get plain warning messages or fatal error messages or nothing at all. This depends on your PHP Server configuration.

Comments 5

  • The Pfizer pharmaceutical corporation introduced a recall of their preferred version of a COX-2 chemical drug named Celebrex, following Vioxx recognition, an identical arthritis pain medication. The Celebrex recall was formally reported Dec. 17, 2004, months after Merckis Sept disengagement. Some have estimated that 45 million people were recommended and obtained Celebrex since its launch in 1999. In exchange, Celebrex people discovered that with continual use, frequently in-patients getting it for over per year, they were given an elevated risk of heart attack, swing, blood clot and also other cardiovascular functions. That evidence raises more issues against Pfizer as to the reasons they extended promoting their seemingly more threatening medication after Vioxx was removed. Celebrex had been used in two reports to locate alternate uses for that medication, such as for instance for cancer prevention. The study displaying a growth in probable heart problems was quit as it persuaded Pfizer to remember Celebrex, though another review did not bring equivalent outcomes. Another test runs on the lower dose of Celebrex and contains not been stopped because no undesirable side effects have now been discovered. Patients who have been taking Celebrex on the doctor’s purchases are recommended to consult with their physician to get a check-up and to discuss alternative painkillers. The Celebrex recognition has additionally increased dialogue about risks invoked by the substance, besides the elevated cardiovascular challenges. Celebrex may cause gastrointestinal ailments, as can many of the known arthritis painkillers, but these unwanted side effects were warned about and acknowledged prior to the release.
    my personal statement

  • We’re safe and sound producing assistance alongside the foremost payment possibilities so you can get faculty papers with no need of going through any issues with just a simply click of the button. Our small business has correctly planted a tranquil backyard garden of exclusive offers you, bonuses and discount rates additionally to speedy and simple payment strategy.
    http://www.xgsz.com/14489.html

  • Punctuation indicates how terms should really be arranged, where the breaks are, and who claimed what. Nevertheless, you’ve to follow along with these idiotic regulations within your entry article. Endmarks (How do grammarians develop this lingo?) 1)Don???t spot two endmarks by the end of a phrase. the quotation marks, as within this example: 4)to Get A word closing having an offer, use question markings or exclamation items in this way: Position the question mark or exclamation place inside the quotation marks if the offered words certainly are a problem or an exclamation. Some examples:
    how to write a college essay
    how to write a science fair research paper powerpoint
    academic assignment writing

  • When faculty and college professors count on their pupils to write down intricate papers, they really don’t trouble outlining how that content material is supposed to get accomplished. They presume college students are previously informed of your expectations of educational composing, so they will ideally straight away commence finishing top-notch articles with no need of making objections. The fact is that, most college students have serious problems with these assignments.
    http://www.picturethailand.com/blog/essay-making-customer-service-decide-to-purchase/

  • It’s difficult to find knowledgeable people today on this topic, but you sound like you understand what you’re talking about! Thanks