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.
Related
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 .
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
I'm just trying to figure out why I can't do this:
<?php
$a = "echo";
$a("test");
?>
It just returns PHP Fatal error: Call to undefined function echo() in Command line code on line 1
When I can do this:
<?php
$a = "explode";
var_dump($a("|","1|2|3"));
?>
and get the expected result.
Edit: Found a solution to my problem (as inspired by the various answers below).
Create an anonymous function inside the variable $a as so:
$a = function($a){echo $a;};
This will only work in PHP 5.3 or greater though.
That's because echo is not a function but a language construct.
Try with print() instead. That should work fine.
EDIT: As pointed out in the comments print is also a language construct and won't work! Only solution is wrapping echo or print in a user defined function then.
<?php
function output($str){
return print $str;
}
$a = "output";
$a("Lorem Ipsum ...");
?>
Echo isn't a function. It's a language construct that resembles a function but it doesn't return any values. Because of this, it cannot be used in variable functions ($foo = 'echo'; $foo ('hello world'); doesn't work)
You will have to use a different output method from echo (such as print) to do what you want.
Maybe because explode is a function whereas echo is a language construct. Notice there are no brackets when using echo.
look in docs
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.
Because echo is not a function, it is a built in part of PHP.
See here: http://www.php.net/manual/en/functions.variable-functions.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
I'm just curious. In PHP, why wasn't echo implemented as a function? Why didn't PHP just give us printf and never tell about echo? Please note that:
This is not a question about echo vs. printf.
I already knew that echo is a language construct.
UPDATE: By the way, was printf implemented using echo?
Echo is not a function and it doesn't return a value like print. Print is a language construct too - does not require parenthesis.
Manual:
echo - No value is returned.
print - Returns 1, always.
The fact remains that returning a value degrades system performance.
So.. now since printf IS a function (which returns the length of the outputted string) the answer I believe is obvious.
Echo is a language construct. Function use language construct to do their job. Explaining is not exactly my specialty, but a google action brought me to this topic:
What is the difference between a language construct and a "built-in" function in PHP?
Some important content:
...
This is the root of why you can't redefine language constructs like echo or print:
they're effectively hardcoded into the parser, whereas functions are mapped to a
set of language constructs and the parser allows you to change that mapping at
compile- or runtime to substitute your own set of language constructs or expressions.
...
Just a wild guess, but perhaps it's because PHP used to exist as CGI binaries. So it would be for making porting shell scripts easier, since you could use the echo binary in those.