Skip to content
View in the app

A better way to browse. Learn more.

MSFN

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[PHP] Include(_once) vs Require(_once)

Featured Replies

I'm just trying to figure out what exactly goes on when I put these into my PHP files. At first reaction, they're very similar, both incorporating the target file into the current file.

Just a few questions though:

1) If an include fails, will the script continue? What about for require? (I'm guessing no)

2) Is the code from the included file simply injected into the current file, or is it parsed?

3) Is one preferable to the other?

I've been playing around with some basic PHP, but I seem to be getting stuck on various places where I'm including header files or general helper functions.

Thanks in advance. :)


1) It will depend on what the to-be-included file contains, e.g. if it contains a vital variable that the script depends on, it will most likely fail to execute. But this depends entirely on the situation.

2) The code you include can be two things: 1 - a text file, that will appear on the place where you place the include code. 2- a php file wich needs to be executed, in opposition to a text file, a php file to-be-included does need to have the <? or <?php starting and ?> ending-tags in the file.

3) I don't really know the difference between the include() and require() functions either, I preffer include(), but it could be that require() has a higher level of dependance or something. Check the php.net documentation for that ;)

Edit:

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.

Edited by Martijn

  • Author

Thanks for the answers Martijn. That's pretty much what I had been thinking already - but I wasn't quite sure. :)

for the error:

Warning: include(header1.php) [function.include]: failed to open stream: No such file or directory in E:\Program Files\HTML\Apache2\htdocs\coastal\getstarted.php on line 13

but the script will still continue

As far as im awaare the difference is so.

the require function will halt if the sepcified file is not found, the include function just keeps parsing the file.

Also I think the include will be pasred every time it is seen where require is only parsed once

e.g.

include("somefile.php");

...

include("somefile.php");

will parse the same file twice, once at each include statement

the require statement

require("somefile.php");

...

require("somefile.php");

will only parse somefile.php once and keep the information in the buffer

(I could be wrong on this but I remeber reading it somewhere)

And you could kill the include using an if statement and using exit(); if you really wanted to. That is normally how I do it. Since I generally send an email out to me when the include fails for whatever reason. But I want the script to continue (well depending on the error code) without the user know there is a problem (when I can help it) .

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.