Today,
while We were referencing a book, we found out that echo have a multiple parameters, but with few testing, Its giving us weird behavior.
Here is a test:
echo("one", "two");
And here is another:
echo "one", "two";
The first test case is giving error, but second isn't..Why?
The documentation gives you the answer:
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.
http://php.net/manual/en/function.echo.php
So, in short: because echo is not a function - skip the parentheses. You can use parentheses if you want to echo only one parameter, but it's custom not to.
<?php
echo 'Hello'; //Acceptable
echo('Hello'); //Acceptable
echo 'Hello ', 'world'; //Acceptable
echo ('Hello ', 'world'); //Not acceptable
?>
Related
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
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How are echo and print different in PHP?
As far as I know the difference between print and echo is that print returns a boolean. So whenever I use echo I can use print instead. Still, in all the code examples I'v seen until now (I'm learning PHP) they used echo. Why is that?
EDIT: Maybe the reason is that echo is faster than print (because print returns a value and echo doesn't)? Even though, I guess the speed difference is unnoticeable.
The print returns a value while echo does not making echo slightly faster (not a big deal though). You may check out this post for more:
echo VS print
Other than that, you use echo to output something unlike print to get some return value too. Therefore, echo gets best in the queue but nothing stops you from using print.
This article has explored this question to a greater depth than you may have even known was possible.
From: http://www.faqts.com/knowledge_base/view.phtml/aid/1/fid/40
1.
Speed. There is a difference between the two, but speed-wise it should be irrelevant which one you use. echo is marginally faster since it doesn't set a return value if you really want to get down to the nitty gritty.
2.
Expression. print() behaves like a function in that you can do: $ret = print "Hello World"; And $ret will be 1. That means that print can be used as part of a more complex expression where echo cannot. An example from the PHP Manual:
$b ? print "true" : print "false";
print is also part of the precedence table which it needs to be if it is to be used within a complex expression. It is just about at the bottom of the precedence list though. Only "," AND, OR and XOR are lower.
Parameter(s). The grammar is: echo expression [, expression[, expression] ... ] But echo ( expression, expression ) is not valid. This would be valid: echo ("howdy"),("partner"); the same as: echo "howdy","partner"; (Putting the brackets in that simple example serves no purpose since there is no operator precedence issue with a single term like that.)
So, echo without parentheses can take multiple parameters, which get concatenated:
echo "and a ", 1, 2, 3; // comma-separated without parentheses
echo ("and a 123"); // just one parameter with parentheses
print() can only take one parameter:
print ("and a 123");
print "and a 123";
Using echo is slightly faster than print, though for most purposes it shouldn't matter.
It doesn't change anything. You can use either one or the other. There's no particular use of 1 being returned, everyone use echo by convention, maybe it's historical. It's also faster to write (4 letters instead of 5).
Most of the time it just comes down to personal preference.
However echo can have have more than one parameter and print returns a value.
The short answer to your question is no, it does not matter which you use. There are minor differences, but it is nothing to worry about. I'll highlight some of them below:
print can be used in expressions, while echo cannot. For example, with print, the following is possible: ($foo == true) ? print 'true' : $foo = true, but replacing with echo would cause an error
echo can take multiple arguments, separated by commas, while print cannot. For example, you would do echo "hello", "world";
print always "returns" the value 1
I know that to concatenate strings in php, a dot should be used:
echo 'hello' . ' world'; // hello world
But incidentally i typed this:
echo 'hello' , ' world';
and the result was still hello world without any errors.
Why is it so?
Can we also concatenate using comma?
It's documented in the entry for echo:
void echo ( string $arg1 [, string $... ] )
The two forms are not actually equivalent, since there's a difference in the instant in which functions are evaluated.
No, you cannot concatenate with comma:
<?php
$foo = 'One', 'Two';
?>
Parse error: syntax error, unexpected
','
echo is a language construct, so you don't need parenthesis. But you are "passing" multiple parameters to echo. Think of it as:
echo('hello', ' world');
It's no hidden trick, it's just how echo works. If you have a look at the PHP reference docs for echo, you'll notice that it will echo the list of strings that you throw at it.
echo is a language construct. It is in someway a special function that's defined at Grammar level (I might be wrong on this). It is a function that somehow doesn't follow any of the defined way of defining a function/method as an example and the way of calling them. It "by-passes" some syntax check :)
There's a nice post discussing the difference between language construct & built in functions here in StackOverflow.
echo('hello', ' world');
It's the same as:
echo 'hello', ' world';