Statements with and without parentheses [duplicate] - php

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?

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.

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

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.

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

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.

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.

for echo always use echo "test"
<?php
// this will give you error
echo ("test","test");
//that will work fine
echo "test","test";
?>

Statements having only one parameter, can have also parenthesis, e.g:
echo ('hello world');

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

Related

'echo $var' vs 'echo($var)'

My code editor has a build in snippet for echo that looks like this:
echo($var);
All the snippets that I browse on the internet are formated without parentheses. Like this:
echo $var;
What is the difference? Which one is correct or considered as a better practice?
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. Additionally, if you want to pass more than one parameter to echo, the parameters must not be enclosed within parentheses.
Going through the documentation , you can notice, that is not a problem, but really unnecessary and I believe your code editor have some setting, you may like to change to get it normal .

Why do the"print" and "echo" functions not need brackets around them

Just wondering.
Thanks.
And I'm just putting this so I can reach the 30 characters, ignore this bit :)
Because they are PHP structures (called also constructs) not functions
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.
Because echo does not behave like a function, the following code is invalid.
($some_var) ? echo 'true' : echo 'false';
However, the following examples will work:
($some_var) ? print 'true' : print 'false';
print is also a construct, but it behaves like a function, so it may be used in this context.

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?
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.
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';
}
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.
Single values within parens evaluate to the value itself, so the parens themselves are of no consequence.
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.
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.
for echo always use echo "test"
<?php
// this will give you error
echo ("test","test");
//that will work fine
echo "test","test";
?>
Statements having only one parameter, can have also parenthesis, e.g:
echo ('hello world');
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

PHP: include() bug, or user error? (Passed filename in IF statement)

Alright so I've already created a PHP bug about this but it was marked as bogus, but I can't help but to believe they didn't read it carefully...
Link to Bug #54042
The error is caused by using an include() statement in an IF statement, making the statement believe it's been passed an empty string.
Reviewing the bug will explain everything, including a test script.
Is it a bug, or am I missing something "feature"-wise? This is weird seeing as how assigning the output of include to a variable and then testing that value works just fine (which would be the workaround).
Well, the documentation says:
Because include() is a special language construct, parentheses are not needed around its argument. Take care when comparing return value.
<?php
// 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';
}
?>
So this behaviour is already known and not considered as a bug. You even have an explanation why it works this way:
include does not need parenthesis, so PHP does not really whether you want to do:
include('vars.php')
or
include(('vars.php') == 'OK')
It seems that it is implemented to interpet it as the latter (something like the longest possible match).
Same goes for
$v = include('foo') . 'bar';
which PHP interprets as
$v = include(('foo') . 'bar');
and tries to load foobar.
But the solution is also given: Wrap the whole include statement into parenthesis, then there is only one way how PHP can interpret the call:
$v = (include ('foo')) . 'bar';
(Maybe) off topic: You will find similar "issues" in other parts of PHP. Take variable variables:
$obj->$foo['bar'];
PHP does not know whether you want to access the bar element of the array $foo and use this value as property name:
$obj->{$foo['bar']};
or if you want to access the bar element of $obj->$foo:
{$obj->$foo}['bar'];
In all these cases, there is a default behaviour of the parser. The developers decided for one possibility because they had to. It might not be what you expect, but if you know it, you can deal with it.
This behaviour cannot simply be changed as it might break code that relies on this behaviour.

Creating functions without parentheses in PHP like 'echo'

I was wondering if there is any nice way of writing functions in PHP so that they don't require ( ) around the parameters.
Example:
function sayThis($str) {
echo $str;
}
sayThis "hi!!";
Thanks,
Matt Mueller
There simply isn't. "echo" is more of an operator than a function, so you'd actually need to rewrite the PHP interpreter source in order to introduce new "functions" like those.
Edit: Actually, the more accurate term for "echo" is, as eyze has correctly pointed out, language construct rather than operator. http://php.net/manual/de/function.echo.php provides some more information.
Simple answer, no.
echo is a language construct not a function, hence it doesn't need the parentheses.

Categories