Jump to content
dalawh

require/require_once vs include/include_once

Recommended Posts

I know that require give an error and include gives warnings when something goes wrong. I assume require is used for something that you want on the page like a navigation bar or something that appears on multiple pages that doesn't change. I am trying to use include to use a function in another php file, but it seems it only works if I use all the functions in that php page. Is there any way to only use some of the functions and not all? Also, the once means it only shows up once, so any repeats will not show up. Does this apply to each page or the whole site altogether?

Share this post


Link to post
Share on other sites

The "once" in require/include applies for the current PHP execution. So, if you run example.com/index.php, from the start of this file to its finish, any include/require_once, as well as any include/require_once within the included files, will be included only once.And no, there's no way to only load part of a file or just one or a few functions of a file. The only way to only include some functions is to simply spread the function out into multiple files.

Share this post


Link to post
Share on other sites

There's no requirement that you need to use a certain amount of functions or anything else in an include file. Using require_once or include_once means that if you try to include the file more than once it will not include the file again. The scope of that is a single request. You'll want to use include_once or require_once for files that define functions or classes because it is a fatal error if you try to redefine an existing function or class.

Share this post


Link to post
Share on other sites
The "once" in require/include applies for the current PHP execution. So, if you run example.com/index.php, from the start of this file to its finish, any include/require_once, as well as any include/require_once within the included files, will be included only once. And no, there's no way to only load part of a file or just one or a few functions of a file. The only way to only include some functions is to simply spread the function out into multiple files.
Oh okay. I was thinking about just leaving the main functions in one file and just call them, but looks like that failed.

Share this post


Link to post
Share on other sites

You can wrap function definitions in an if statement if you only want them to be defined in a certain situation.

Share this post


Link to post
Share on other sites
You can wrap function definitions in an if statement if you only want them to be defined in a certain situation.
Yea, I know. I am using the functions multiple times throughout the site.

Share this post


Link to post
Share on other sites
Guest So Called

I put all my functions used all over my site in one PHP file and then I use require_once() in each script that uses the common functions. I prefer require to include because if the file isn't there the script will terminate with an error. The reason I prefer that is that my script won't work without the functions and I don't want to cause multiple errors. I prefer to test if the file exists before requiring it, so that if it doesn't I can generate my own error message rather than falling back to PHP's error message. Note that the PHP error message includes path information, and I prefer not to show path information to visitors for security reasons. So it's sort of like this:

if (file_exists("filename.php")) require_once("filename.php");else die("some error message");

Share this post


Link to post
Share on other sites
I prefer not to show path information to visitors for security reasons.
You shouldn't show any PHP errors to your visitors at all, that's what error logging is for.

Share this post


Link to post
Share on other sites
Guest So Called

That's more or less what I meant. I try to capture every possible error from within my code so that I can generate whatever error messages I want, rather than letting a visitor see a raw PHP error message. Other than when debugging code I haven't seen a PHP generated message on my site in years.

Share this post


Link to post
Share on other sites

I'm saying there are settings in PHP to do that, so you don't have to capture and handle every error: ini_set('display_errors', 0);ini_set('log_errors', 1);ini_set('error_log', dirname(__FILE_) . DIRECTORY_SEPARATOR . 'error.log');error_reporting(E_ALL); On your development server you change the settings to display errors, on the production server you log them. You can set those in php.ini also, except it is useful to have a dynamic error log location.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...