Jump to content

require/require_once vs include/include_once


dalawh

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?

Link to comment
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.

Link to comment
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.

Link to comment
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.
Link to comment
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");

Link to comment
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.

Link to comment
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.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...