The echo and print statements in php [duplicate] - php

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.

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

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

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.

One line code to get one value from function that returns an array [duplicate]

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

exit(); die(); return false; [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
what are the differences in die() and exit() in PHP?
I guess the main question is what is the difference between the 3 exactly?
What is the correct semantic usage for each one of these?
From what I see return false; can discontinue a function whereas die(); and exit(); will prevent any further code running at all.
Is this correct?
die() and exit() are precisely identical; they halt the entire PHP program and return to the OS. They're two different names for the same function.
return, on the other hand, ends a function call and returns to the caller. At the end of a program, return sets the status value that is returned to the OS; the program is going to exit no matter what.
According to docs PHP: exit Manual die() is an alias to exit() so they do the same function and that is to END the script.
The return statement ends a function and not the entire script, and returns the value that you choose.

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