Mr_Chimp
Mr_Chimp

Reputation: 6917

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

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?

Upvotes: 20

Views: 6760

Answers (9)

user1786283
user1786283

Reputation:

The parentheses are parameters for a function. With include you can use it either as a function or a statement in php.

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

Documentation here: http://php.net/manual/en/function.include.php

With echo same concept, quoting from the PHP manual here

echo is not actually a function (it is a language construct), so you are not required to use parentheses with it. echo (unlike some other language constructs) does not behave like a function, so it cannot always be used in the context of a function.

Upvotes: 6

Akshay Deep Giri
Akshay Deep Giri

Reputation: 3280

for echo always use echo "test"

<?php 
// this will give you error
echo ("test","test");

//that will work fine
echo "test","test";
?>

Upvotes: 0

Mattios550
Mattios550

Reputation: 372

I believe the only difference is what you want to do.

For example, these two

echo 'Hello World';
echo ('Hello World');

...both print Hello World.

It is just what you want to do, what you feel most comfortable with, and if you're like me, you want to make your script look pretty :D

Upvotes: -1

icktoofay
icktoofay

Reputation: 129001

include, require, echo, print, and a handful of other keywords are language constructs on their own, rather than function calls, and do not need parentheses. However, some people like to pretend they're function calls and use parentheses anyway. They are identical.

Upvotes: 1

Mark Baker
Mark Baker

Reputation: 212412

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.

Upvotes: 30

leebriggs
leebriggs

Reputation: 3257

Both. In many areas of the PHP documentation, everything is referred to as a statement. Example (from control structures) - "A statement can be an assignment, a function call, a loop, a conditional statement or even a statement that does nothing (an empty statement)."

The difference between a statement and a functions is a matter of the semantics of the individual language. Thus, it's up the PHP maintainers to either explicitly define this, or let it remain ambiguous.

Upvotes: 1

Manish Trivedi
Manish Trivedi

Reputation: 3559

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';
}

Upvotes: 9

Simon
Simon

Reputation: 9365

Statements having only one parameter, can have also parenthesis, e.g:

echo ('hello world'); 

Upvotes: -1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798686

Single values within parens evaluate to the value itself, so the parens themselves are of no consequence.

Upvotes: 1

Related Questions