Verification: a143cc29221c9be0

Php check if not included

Example 1.

The following code will evaluate to true because the main string $str contains the substring ‘This‘ in it. This will print “true”.

$str = 'This is Main String';

if (strpos($str, 'This') !== false) {

    echo 'true';

}

?>

Output: true

Example 2.

The following code will evaluate to false because the main string $str doesn’t contains the substring ‘Hello‘ in it. This will nothing print.

$str = 'This is Main String';

$substr = "Hello";

if (strpos($str, $substr) !== false) {

    echo 'true';

}

?>

Output: none

Example 3.

The following code will check if a String contains a substring at start. The following code will evaluate to true because the main string $str contains the substring ‘This‘ at start.

$str = 'This is Main String';

if (strpos($str, 'This') === 0 ) {

    echo 'true';

}

?>

Output: true

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.

Return values

Returns an array of the names of all files.

The script originally called is considered an "included file," so it will be listed together with the files referenced by include and family.

Files that are included or required multiple times only show up once in the returned array.