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 9

  • 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

  • Recall the occasions in the past once you battled to master anything–cycling or skating for example–and also you assumed it was too hard. Then anything created you grit your teeth and continue attempting until abruptly, you made it happen! Quality No 4: Target I have seen many actually smart people flounder simply because they dropped their focus and became diverted from their studies. Focus on the large treasure. Quality Number 5: Readiness It will offer you patience and invite you to discuss the down sides of graduate school with equanimity. It’ll cause you to stand up every time you drop. And Lastly… Do not dwell on your own defects and defects. Become self-regulatory and self-upgrading and stop trying to find direction and supervision. I do you know what I am actually attempting to declare listed here is that the scholar attributes desired when likely to grad school are all faculties you will obviously uncover if you listen to oneself and find out who you actually are. Graduate School Information Articles in this information include information about how to apply to your graduate plan, a schedule of the applying approach, a schedule of the measures you will need to take before concluding grad-school, the troubles you’ll experience in graduate school and the personal attributes you’ll require to achieve success.
    me writing an essay

  • 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

  • Inside the structure spot, issues never appear very simple. And undoubtedly the intricate concerns of building in disagreements or with defects. Therefore, when just to contact a structural engineer Brighton located? For example, for structural measurements, project-management, for preparation of work requirements for building tasks or for deficiency diagnosis and reporting diagnosis, it is needed to get hold of a licensed architectural engineer Brighton hosts! From what it seems, it???s not just a lawful necessity to employ a licensed surveyor Brighton found but it???s obligatory for guarantying the achievement of your project. In the long run, owning a project in constructions is not easy at all: it requires specialized help and particular capabilities. Every one of these and much more can be obtained by way of a certified surveyor in Brighton. However, there is one primary problem: if you like to take advantage of the greatest technological help, then you should retain seasoned workers and not just any inexperienced.
    assignment help review
    short personal statement for college
    academic personal statement

  • 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/

  • Can you discover beyond my miserable and leery eyes? Can you see solely my concerned encounter and securely tucked tail? Recently I’d a quite wagging tail as well as a spark in my own wonderful copper eyes. The picture witnessed here was consumed around the time that I shed my household…around the morning that my world turned upside-down. Can you view beyond my hurt spirit? Info: Rimyis Petharbor link here RIMY – ID#A4798393 Era 2 L A County Animal Control – Baldwin Park at (626) 962-3577 Facebook line below All queries must be aimed for the Baldwin Park, California, capability – the Nationwide Pet News Examiner is NOT the contact Note – operator surrenders are not placed so long as strays…Rimy’s time is bound
    what a personal statement should look like
    lab report joke
    buy cheap essays online

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

  • An fascinating dialogue is value comment. I believe that you need to write extra on this matter, it might not be a taboo subject but generally persons are not sufficient to speak on such topics. To the next. Cheers