[Solved] What is the difference between include and include_once with and without round brackets


The parentheses around the filename make no difference. include is a language construct, not a function, so it doesn’t require parenthese around its arguments. So they’re equivalent, just like:

echo "foo";
echo ("foo");

So there are just two forms: include and include_once. The difference between them is what happens if you try to include the same file multiple times in the same script. include will include it each time, include_once will only do it the first time, and ignore all the repeats.

You generally use include if the file performs some action, such as echoing HTML or setting variables. include_once is used for files that define functions and classes, because these will get an error if you try to redefine them.

If the file doesn’t exist, you’ll get warnings like this:

Warning: include(file.txt): failed to open stream: No such file or directory in /path/to/your/script.php on line 4
Warning: include(): Failed opening ‘file.txt’ for inclusion (include_path=”.:”) in /path/to/your/script.php on line 4

The warning is the same for include and include_once.

There’s also require and require_once. They’re just like include and include_once, but instead of printing a warning if the file doesn’t exist, it prints an error and stops the script.

3

solved What is the difference between include and include_once with and without round brackets