PHP Programming: The difference between require() and include()


php, programming, php programming, open source, php function
Many friends ask me what’s the difference between require() and include(). Here is the explaination from PHP manual.

From PHP manual:-

require() and include() are identical in every way except how they handle failure. include() produces a Warning while require() results in a Fatal Error. In other words, don’t hesitate to use require() if you want a missing file to halt processing of the page. include() does not behave this way, the script will continue regardless. Be sure to have an appropriate include_path setting as well.

Advertisements

So that means, if you want to ensure that file1.php must be read before execute anything else, then you should use require.

If you just want to load a ‘not that important’ template header then you can use include function.

[tags]php, php function, include, require, function include, function require, programming, open source[/tags]




Share this with your friends:-

11 Responses to “PHP Programming: The difference between require() and include()”

  1. chua says:

    hi Ellsworth Baldos, my site don’t serve any pop under or pop up advertisement.
    so it might be your machine infected by malware.

  2. Hello i just came across your blog from Google but encountered a odd popup about dog hospitals which is obviously not related to your site. Is this your ads or do i have a spyware issue? Thanks.

  3. Kimberly says:

    Great!!!

  4. Rajesh Meniya says:

    hello,
    this is very useful for php programmer.

  5. gizg: pclfw’s post is incorrect. When using require, PHP will not look for them before parsing the document. An easy way to test this is the following code:

    File: test.php

    File: no.php

    File: yes.php

    Create these files with this code and then visit test.php. What happens? The page displays NO! because it only got no.php

  6. gizg says:

    This was good, but, I have to say that post #4 (by “pclfw”) on this page actually gives a better explanation:

    http://www.daniweb.com/forums/thread25930.html

  7. dileep says:

    i want php programming with explanation

  8. Norhan says:

    it’s the simplest way i’ve ever read about that differece

    thanks for your simple explanation

  9. adriene says:

    Please, if you have free time, visit one website. I’d be grateful as once I applied at a website and got bad experience. The card didn’t come, maybe even someone stole it from postbox. But my friend recommended another site he recently found. Please, tell me if I can apply for a good credit card at
    ?
    balance transfer credit application

  10. Zebra says:

    Indeed !!!

    Finally a simple explanation for a simple thing.

    Thank You

  11. vinod says:

    HEllo dear,

    This is the very good and simple diff. every one can understood it from very easily.

    Thanks vinod

Leave a Reply