Showing posts with label PHP require_once. Show all posts
Showing posts with label PHP require_once. Show all posts

Monday, 17 September 2018

Testing if a PHP include file was included

PHP's include() and include_once() functions can return values to the calling script which can be useful for testing to see if the file was actually included, for example if a configuration file whose filename is based on the website's domain name was included. If no return value is set then it will return integer 1 if included, or boolean false if not.

Code example

So you could do this, for example:
if(!@include_once('/path/to/some-script.php')) {
  // do something
}
The @ symbol suppresses warnings from being displayed if error reporting permits errors of E_WARNING to be reported and display_errors is on.

Using return

The included file can return a value to the calling script, for example like so:
return 1;
Just like in a function call, execution will return from the include file to the calling script at the point return is called, so no further code in the file will be executed.
If you returned 0 from the include file, then in the first example the code in the curly braces would be executed.
As noted in the opening paragraph, if no return value is set in an include file then it will default to integer 1 after the code executes or boolen false if the file could not be included.

require() and require_once()

Returning values works the same way for require() and require_once() as it does for include() and include_once(), also defaulting to integer 1 if not set.
However, a fatal error is issued if a file cannot be required so you can't test for it being required in your code.

Related posts:

Wednesday, 12 September 2018

Get the included files with PHP

PHP's get_included_files() function returns an array containing a list of included files by your script, including the script itself. This post looks at the filenames returned in the array and corrects a couple of errors in the PHP documentation for this function.
Calling get_included_files() returns an array containing all the included files whether they were included with the include() include_once() require() or require_once() functions. It also includes the script itself, and any prepended scripts using the auto_prepend_file configuration directive (note that in the PHP documentation it says these are not included in the result). It does not include scripts included with the auto_append_file configuration directive.
For example, if you had the following script at /common/websites/test/example.php:
<?php
include('included.php');
include_once('included_once.php');
require('required.php');
require_once('required_once.php');
$included = get_included_files();
print_r($included);
The output of the above script would be:
Array
(
    [0] => /common/websites/test/example.php
    [1] => /common/websites/test/included.php
    [2] => /common/websites/test/included_once.php
    [3] => /common/websites/test/required.php
    [4] => /common/websites/test/required_once.php
)
If a file is included more than once it will be in the array just once.
If an included file did not exist and could not be included it will not be in the array.
The full path to the included files is included in the filenames. This is a second issue with the documentation on the PHP website: the example provided in the docs shows only the filename and not the full path as well. Perhaps this was the case in earlier versions of PHP, but at the very least in PHP 5.1.6 on CentOS and 5.2.6 on Debian it has the full path as well.

Related posts: