PHP: Can't access a definition from an included file?

130 views Asked by At

I have a file file_to_include.php that contains variable definitions,

define("NAME", "John Doe");

But when I try to include it in my other file, index.php, using

include_once 'file_to_include.php';

and then echo my definition in index.php with

echo NAME;

All I see on the page is NAME, not John Doe.

I think definitions are global, what am I doing wrong?

1

There are 1 answers

0
alpham8 On

Maybe wrong path, look what the PHP's inlcude_path is look like. PHP's include and include_once will continue, even the requested file is found or not.

You should keep that in mind, that require and require_once will break the execution if the file is not found, in contrast to include and include_one will not.

The PHP documentation says:

require is identical to include except upon failure it will also produce a fatal E_COMPILE_ERROR level error. In other words, it will halt the script whereas include only emits a warning (E_WARNING) which allows the script to continue.