This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Strange echo, print behaviour in PHP?
The following PHP code:
print (2).(3 * (print 3));
displays "323" to the output? How is it processed?
This is because of the brackets (operation precedence) - the
(print 3)
in the end of the line displays the first digit of the final output (3), but all PHP print statements return 1. Always (check the manual). So after this, we've got:
print (2).(3 * 1);
which is the same as:
print (2).(3);
Now it's just a simple concatenation which will output "23". So we've got "323" displayed.
Note that
print (2).(1 - (print 3));
would display "320".
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:
PHP printed boolean value is empty, why?
(4 answers)
Why doesn't PHP print TRUE/FALSE? [duplicate]
(3 answers)
Closed 3 years ago.
Using == vs Using !=
/*Testing some basic scenarios in PHP*/
$a=7;
$b=5;
echo $a==$b;
$a=7;
$b=5;
echo $a!=$b;
Output is blank screen in case 1.
Output is 1 in case 2
.Why am I getting these respective outputs?
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 6 years ago.
error message when trying to print the next position in loop?
$coba = "testing";
for($h=1;$h<(count($coba));$h++){
echo $coba[$h+1];
}
If this is your code, you would get that error:
$coba = "testing";
for($h=1;$h<strlen($coba);$h++){
echo $coba[$h+1];
}
The reason is you're trying to print something that don't exist.
You are incrementing $h to a number greater than the array. That's why you are getting the offset error.
Original code is incorrect here --> for($h=1;$h<(**count**($coba));$h++){
See the PHP reference guide for difference in count() and strlen().
count($coba) is 1
strlen($coba) is 7
If you use strlen then the code would correctly loop through the string:
$coba = "testing";
for($h=1;$h<strlen($coba);$h++){
echo $coba[$h+1];
}
Now, regarding the error you mentioned, when I run the above I get:
PHP Notice: Uninitialized string offset: ...
Two problems here with your original code. Since I do not know your original intent, I can only guess at what you were trying to do with the loop.
Problem #1: indexing into the string should start with 0, not 1
Problem #2: $coba[$h+1] will be an invalid index at the END of the array at h+1.
You can either adjust the indexing h+1 to just be h OR
change the loop to loop 1 less via for($h=0;$h<(strlen($coba)-1);$h++){
Thus, final code could look like:
$coba = "testing";
for($h=0;$h<(strlen($coba)-1);$h++){
echo $coba[$h+1];
}
Which outputs when run:
esting
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:
Strange echo, print behaviour in PHP?
(4 answers)
Closed 9 years ago.
How does this statement get evaluated to produce 234.
echo '3'.(print'2')+3; // output 234.
You are using the echo command to display a string. This string is a concatenation of a string "3" and the result of the call to print '2' added with the number 3.
During the concatenation print '2' is evaluated, thus the output starts with that. Afterwards the concatenated string '34' is printed, where the 4 is the result of 1 (=true, the result of the print call) + 3
Way too much text, but I hope that covers it all.