Php echo and print different behavior in for loop? [duplicate] - php

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.

Related

Which kind of purpose was for returning 1 in print from PHP lenguage? [duplicate]

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

Whats faster: echo or print? - PHP [duplicate]

This question already has answers here:
Reference: Comparing PHP's print and echo
(2 answers)
Closed 7 years ago.
Which statement should I be using in PHP? Print or Echo?
In PHP, Echo and Print do the same thing.
Whats the difference? is one of them faster?
Thanks!!
This is a duplicate. Please refer to:
How are echo and print different in PHP?

What Is Difference Between The echo() Statement And The print() Statement In PHP? [duplicate]

This question already has answers here:
Reference: Comparing PHP's print and echo
(2 answers)
Closed 8 years ago.
<?php
print();#The Print Statement
echo();#The Echo Statement
?>
I Would Be Very Thankful If Someone Answered My Question...
print returns a value (always 1); echo returns nothing
echo will accept multiple arguments, print only accepts one argument

The echo and print statements in php [duplicate]

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.

Differences between echo, echo(), print and print() in PHP [duplicate]

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.

Categories