Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is php's 'include' a function or a statement?

Tags:

There are plenty of examples of both on the web. The php manual says "The include() statement [...]", which seems contradictory - if it's a statement shouldn't it not have parenthesis?

Both of these work:

include('somefile.php');
include 'somefile.php;

So should I or anyone else care?

like image 370
Mr_Chimp Avatar asked Feb 10 '11 10:02

Mr_Chimp


People also ask

How is include () different to require ()?

include() Vs require() The only difference is that the include() statement generates a PHP alert but allows script execution to proceed if the file to be included cannot be found. At the same time, the require() statement generates a fatal error and terminates the script.

What is the difference between include and include_once in PHP?

The include() function is used to include a PHP file into another irrespective of whether the file is included before or not. The include_once() will first check whether a file is already included or not and if it is already included then it will not include it again.

What is the statement in PHP?

PHP Conditional Statementsif statement - executes some code if one condition is true. if...else statement - executes some code if a condition is true and another code if that condition is false. if... elseif...else statement - executes different codes for more than two conditions.

What is the difference between include () function and require () function in PHP?

Typically the require() statement operates like include() . The only difference is — the include() statement will only generate a PHP warning but allow script execution to continue if the file to be included can't be found, whereas the require() statement will generate a fatal error and stops the script execution.


2 Answers

Quoting from the manual (my emphasis)

Because include() is a special language construct, parentheses are not needed around its argument.

These are also called "special forms", and include such things as echo and return statements. Note that while none of these are functions, you can still speak of expressions and statements, the difference being the former have a value while the latter don't. Since include, include_once, require and require_once all return a value (TRUE if the include was successful), they can be used in expressions. By this reasoning, "include statement" would be incorrect, though includes are almost always used as statements.

like image 94
Mark Baker Avatar answered Sep 27 '22 18:09

Mark Baker


include is a statement : Explain by following eg

// won't work, evaluated as include(('vars.php') == 'OK'), i.e. include('')
if (include('vars.php') == 'OK') {
    echo 'OK';
}

// works
if ((include 'vars.php') == 'OK') {
    echo 'OK';
}
like image 39
Manish Trivedi Avatar answered Sep 27 '22 18:09

Manish Trivedi