This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
How are echo and print different in PHP?
Is there any difference between ‘print’ and ‘echo’ in PHP?
What’s the difference of echo,print,print_r in PHP?
There are several ways to print output in PHP; including but not limited to:
echo 'Hello';
echo ('Hello');
print 'Hello';
print ('Hello');
Are there any differences between these four? Also, do the parentheses make any difference at all?
Two differences:
print has a return value (always 1), echo doesn't. Therefore print can be used as an expression.
echo accepts multiple arguments. So you may write echo $a, $b instead of echo $a . $b.
Concerning the parentheses: They are simply wrong in my eyes. They have no function at all. You could as well write echo (((((((((($a)))))))))); people usually include parentheses from ignorance, thinking that print is a function. Furthermore it increases the chance of misinterpretation. For example print("foo") && print("bar") does not print foobar, because PHP interprets this as print(("foo") && print("bar")). So bar1 would be printed, even though it looks different.
Related
This question already has answers here:
Why does print return (int) 1?
(6 answers)
Closed 2 years ago.
Could you explain why 'print' in PHP return 1?
For what? Was it the result of simple creativity? Or maybe, "we have created, and you can do with that whatever you wish"?
For example,
$a = print('4');
var_dump($a);
From the docs
Returns 1, always.
Why Should I use print? What kind of Advantage?
Both print and echo are language constructs and echo is even faster than print. But the advantage of print is that it is more flexible than echo and it can do anything that echo can do.
The simplest example is, print can be used in an expression like ternary.
($someValue) ? print('somevalue is True') : print('someValue is false');
This is because as I stated above print always returns 1 and echo doesn't have a return value.
Read Official Documentation Here:
PHP print
PHP echo
This question already has answers here:
Curly braces in string in PHP
(4 answers)
Closed 6 years ago.
Sometimes you need to make clear to PHP what is actually the variable name. I have discovered that a colleague and I had been doing it slightly differently.
Say you had a variable $foo and wanted to output that with _constant_string appended
I had been using
return "<input type='hidden' name='${foo}_constant_string' value='true' />";
whereas my colleague is using
return "<input type='hidden' name='{$foo}_constant_string' value='true' />";
(slightly contrived example to simplify it).
My quick tests don't reveal an obvious difference, but I am curious: Is there a difference? Is there a reason to prefer one over the other?
Edit:
My example above used strings but my question was more general - I should have explicitly said so. I knew you could use curly braces for escaping, but hadn't found the specific point of if there was (in any situations) differences between the two ways of using them. I got the answer: there isn't for strings (which is what the "duplicate" post is about) but is for arrays and objects (thanks #dragoste).
It seems, there is no difference in any PHP version
$foo = 'test';
var_dump("$foo");
var_dump("{$foo}");
var_dump("${foo}");
Test: https://3v4l.org/vMO2D
Anyway I do prefer "{$foo}" since I think it's more readable and works in many other cases where other syntax doesn't.
As an example let's try with object property accessing:
var_dump("$foo->bar"); //syntax error
var_dump("{$foo->bar}"); // works great
var_dump("${foo->bar}"); //syntax error
The same case are arrays.
http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing.complex
No, there is no differentce.
// Works, outputs: This is fantastic
echo "This is {$great}";
echo "This is ${great}";
Php manual
Answer on stackoverflow
Another way use it for variable:
$foo = 'test';
$test = 'foo';
var_dump("{${$foo}}"); //string(3) "foo"
Or for array:
$foo = ['foo','test'];
var_dump("{$foo[0]}"); //string(3) "foo"
var_dump("${foo[1]}"); //string(4) "test"
There is no difference between the following statement -
echo "This is {$great}";
echo "This is ${great}";
The output of both statements will be same.Please check following example-
$great = 'answer';
echo "This is {$great}"."\n";
echo "This is ${great}";
Output:-
This is answer
This is answer
This question already has answers here:
Reference: Comparing PHP's print and echo
(2 answers)
Closed 8 years ago.
Example 1
for($var=1;$var<=5;print $var,$var++); //valid
Example 2
for($var=1;$var<=5;echo $var,$var++); //invalid
the behavior of above two statements is not that straight, could any body explain why they are showing different results ?
echo is a language construct, not a function. It has no return value. print() is a function, and DOES have a return value.
Ref: http://php.net/echo http://php.net/print
While both print and echo are language constructs, the syntax defined for echo conflicts with what you are doing. Specifically:
echo 1, 2, 3, 4;
//output: 1234
This conflicts with the syntax for a loop definition, which is why I believe you're not permitted to use echo there.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Parse error on explode('-','foo-bar')[0] (for instance)
In PHP there are functions that return an array, for example:
$a = parse_url("https://stackoverflow.com/q/9461027/87015");
echo $a["host"]; // stackoverflow.com
My question is how to combine the above two statements into a single statement. This (is something that works in JavaScript but) does not work:
echo parse_url("https://stackoverflow.com/q/9461027/87015")["host"];
Note: function array dereferencing is available since PHP 5.4; the above code works as-is.
You can do a little trick instead, write a simple function
function readArr($array, $index) {
return $array[$index];
}
Then use it like this
echo readArr(parse_url("http://stackoverflow.com/questions/9458303/how-can-i-change-the-color-white-from-a-uiimage-to-transparent"),"host");
Honestly, the best way of writing your above code is:
$a = parse_url("http://stackoverflow.com/q/9461027/87015");
echo $a["scheme"];
echo $a["host"];
Isn't that what I originally posted?
Yes. Depending on context you may want a better name than $a (perhaps $url), but that is the best way to write it. Adding a function is not an attractive option because when you revisit your code or when someone reads your code, they have to find the obscure function and figure out why on earth it exists. Leave it in the native language in this case.
Alternate code:
You can, however, combine the echo statements:
$a = parse_url("http://stackoverflow.com/q/9461027/87015");
echo $a['scheme'], $a['host'];
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is basic difference echo Vs print
The echo and print statements are nearly identical, but they have some differences.
For example, the print statement returns a value of 1 if it is successful
or a value of 0 if it is not successful, while the echo statement does
not return a value.
why print statement returns a value of 1 if it is successful. but echo doesn't. thank you
I've actually taken advantage of the return value of the print "function" in an ajax call:
return print json_encode($my_data);
It doesn't do anything at all with the return value, but it terminates the execution of the current script, which is a slightly prettier way of writing
echo json_encode($my_data);
die();
But as to why one returns something and the other doesn't.... probably isn't a terrible good reason for it. I reckon echo is ever so slightly (negligibly) faster because of it, whereas print has weird uses such as the aforementioned one.
As to what these other guys are saying about print() not being a language construct, but a function, I say to you, read the manual. It's a language construct too.