Explanation for PHP print adding weirdness [duplicate] - php

This question already has answers here:
Why does "echo '2' . print(2) + 3" print 521? [closed]
(4 answers)
Closed 9 years ago.
This is related to:
Why does "echo '2' . print(2) + 3" print 521?
I was wondering why
print(99) + print(99) + print(99) + print(99);
shows 99100100100....
I'd like to learn about the quirks of PHP.

Print is not a function, so print(99) is the same as print 99. We can remove the parentheses for clarity.
print 99 + print 99 + print 99 + print 99;
The expression is evaluated from the right, so it becomes
print (99 + print (99 + print (99 + print 99)));
The rightmost print executes first, printing "99" and evaluating to 1.
Output:
99
Code left to be evaluated:
print (99 + print (99 + print (99 + 1)));
Again, the rightmost print is executed and it prints 99+1 ("100") and evaluates to 1.
Output:
99100
Code left to be evaluated:
print (99 + print (99 + 1));
...and so on.

The print function produces output and then (see documentation) returns the value 1. To further complicate things, the print function is a language construct (see the documentation) which does not require the parenthesis.
So, if you had the statement
print(99) + print(99);
What you are actually seeing is the printing of a 99, from the right-most print, followed by the result of print(99) + 1; which, due to the language construct, is the same as print 99 + 1; or print (99 + 1);.

Because, from the docs,
print …
Returns 1, always

Because print() always returns 1. Check the php manual
This is unlike echo() which does not return the value.
Both print() and echo() are not actual functions but language constructs.

Related

$variable after <br> is not showing in echo in php

I'm learning basics in php, and I was trying to add multiple arguments in echo command. But the variable after <br> is not showing.
$number1=10;
echo "number 1 is: ".$number1."<br>";
$number2=20;
echo "number 2 is: ".$number2;
echo "<br> ".$number1+$number2;
and the output should be:
number 1 is: 10
number 2 is: 20
30
But the output is:
number 1 is: 10
number 2 is: 2020
So what's the error?
used this code
$number1=10;
echo "number 1 is: ".$number1."<br>";
$number2=20;
echo "number 2 is: ".$number2;
$total= $number1+$number2;
echo "<br> ".$total;
?>
The output will be:
number 1 is: 10
number 2 is: 20
30
The other answers just state a solution, this answer explains whats happening and how to prevent the unexpected behavior in 2 ways.
The dot operator has the same precedence as + and -.
Considering
$number1 = 10;
$number2 = 20;
echo "<br> ".$number1+$number2;
The dot you've used is a string operator, not a numeric operator.
What's happening:
"<br>" and 10 are concatenated with the dot operator to "<br>10".
"<br>10" is added to $number2 (20) with the numeric + operator.
Non-empty, non-numeric strings are converted to 0. Meaning "<br>10" = 0.
0+20 results in 20 which makes line 3: echo 20;
This can be solved by changing the precedences by using brackets echo "<br> ". ($number1 + $number2); or the less seen option, by passing more arguments to the echo language construct: echo "<br> ", $number1 + $number2; (Note the comma instead of a dot). Each argument will be evaluated first before outputting them all together.
Personally I use the second option (multiple arguments) in cases like this.
Just add the braces to the sum operation.
$number1=10;
echo "number 1 is: ".$number1."<br>";
$number2=20;
echo "number 2 is: ".$number2;
echo "<br> ".($number1+$number2);
The output will be:
number 1 is: 10
number 2 is: 20
30
You should revise code within bracket
echo "<br> ". ($number1 + $number2);
to get the result you want.
Reason: each operation has precendence level
Get reference: http://interactivepython.org/runestone/static/pythonds/BasicDS/InfixPrefixandPostfixExpressions.html

PHP echo, how it's produce the output? [duplicate]

This question already has answers here:
Concatenation with addition in it doesn't work as expected
(2 answers)
Closed 7 years ago.
Please explain how echo understand the dot(.) with mathematical expressions and binary comma(,).
<?php
echo "The Sum: " . 2+3;
?>
//Output
3
Why 3 as output?
. and + are left-associative, so your statement is interpreted as
echo ("The Sum: " . 2) + 3;
This is equivalent to
echo "The Sum: 2" + 3;
When you add a string and a number, it converts the string to a number, which tries to find a number at the beginning of the string. Since "The Sum: 2" doesn't begin with a number, it converts to 0. So that makes the statement equivalent to
echo 0 + 3;
which simplifies to
echo 3;
and that's the result you see.
there is two operator dot(.) and plus(+) and dot has high priority so . try this
<?php
echo ("The Sum: " . 2) + 3;
?>

What is the output of the following PHP code?

the output of the following code?
echo '1' . (print '2') + 3;
I tested and the result is 214, but why 214?
if I code:
echo (print '2') + 3;
the result is 24
Then, echo '1' . '24'; should be 124.
Confused...
When the expression is parsed, the "print" statement is immediately writing its output. So there's the first 2. By definition its return value is 1.
So then the remaining expression is the character 1, followed by the numeric expression 1+3. Therefore 1 and 4.
214

Explain the output of : echo (2) . (3 * (print 3));

I know the above code outputs 323
But need an explanation on how is it outputting 323
Anybody can pls explain?
Both echo and print are intended to behave more like language constructs and not functions. As such they have a flow of control. What happens here in your code is that you are calling print from inside of a language construct (echo). Meaning that print will send its output first before echo has completed its task (remember you called print from inside of echo).
To show you what's happening a bit more clearly, it actually has nothing to do with operator precedence at all.
echo ('a') . ('b' * (print 'c')); // ca0
// This is the same thing as...
echo 'a' . 'b' * print 'c'; // ca0
Notice the operators have no effect on the order of the characters in the resulting output here.
print always returns 1 so what happens here is that you performed an arithmetic operation on 'b' * 1, which is the stirng b multiplied by the return value of print. Thus why you see the output as c (print sent output before echo has even finished doing it's job), first, and then everything that echo was supposed to print.
Let me elaborate even further with the following example...
echo print 'a' . 'b' . 'c'; // abc1
Notice the 1 at the end of that output. This is because all echo did was output the return value of print and not the string. Instead, print is the one that provided the abc as output (and remember print is able to send output before echo can since echo has to wait to process everything inside of the construct before it can finish).
This makes it even more clear...
echo (print 'a') . 'b' . 'c'; // a1bc
Now the 1 comes right after a.
If you want echo to send the output for each expression individually, you can supply one argument for every expression you want processed and sent to output... So for example:
echo print 'a', 'b', 'c'; // a1bc
echo 2, 3 * print 3; // 233
I hope that clarifies it a bit more for you.
The reason is (print 3) has priority over the previous operators. If you write `echo (8) . (7 * (print 3)); you will get 387, for example.
Here
echo (2).(3*print(3));
Step1:(Starting execution)
echo (2).(3*print(3)); //Output = ''
Step 2:(Print action will take place)
echo (2).(3*1); // Will print 3 and returns 1 as per print function. Output = 3
Step 3:(Multiplication operation)
echo (2).(3); // Multiplication operation willtake place. Output = 3
Step 4:(Prints the data)
echo (2).(3); //The . Operator will used to concate the strings in php, thus Output = 323
Print has more priority then echo thus it prints 3 first later 2 and 3

Combine an echo and a print in one statement

echo "1" . (print '2') + 3; returns 214. How does the script end up with *14?
When you do
echo "1" . (print '2') + 3;
PHP will do (demo)
line # * op fetch ext return operands
---------------------------------------------------------------------------------
2 0 > PRINT ~0 '2'
1 CONCAT ~1 '1', ~0
2 ADD ~2 ~1, 3
3 ECHO ~2
4 > RETURN 1
In words:
print 2, return 1
concat "1" with returned 1 => "11"
add "11" + 3 => 14
echo 14
and that's 214.
The operators + - . have equal Operator Precedence, but are left associative:
For operators of equal precedence, left associativity means that evaluation proceeds from left to right, and right associativity means the opposite.
Edit: since all the other answers claim PHP does 1+3, here is further proof that it doesnt:
echo "1" . (print '2') + 9;
gives 220, e.g. 11+9 and not 1 . (1+9). If the addition had precedence over the concatenation, it would have been 2110, but for that you'd had to write
echo "1" . ((print '2') + 9);
echo "1" . (print '2') + 3;
You need to think of it in a logical order, of what happens first.
Before we can echo anything, the - "1" . (print '2') + 3 - we need to evaluate it to solve it.
First we write 1 down on a scrap of paper as the first part of our calculation.
Scrap paper: 1
Answer Sheet:
We calculate "print '2'", which as a function writes the number 2 to our sheet of answer paper and returns a 1, which we write on our scrap piece of paper.
Scrap paper: 1 . 1 +3
Answer Sheet: 2
After this we want to concatenate the next piece on to the end, due to the "."
Scrap paper: 11 + 3
Answer Sheet: 2
Now we put it together
Scrap paper: 11 + 3
Scrap paper: 14
Answer Sheet: 2
Then we echo out our scrap data to our answer sheet
Answer Sheet: 214
echo "1" . (print '2') + 3;
1.
Code--: echo "1" . (print '2') + 3;
Result:
2.
Code--: echo "1" . 1 + 3;
Result: 2
3.
Code--: echo 11 + 3;
Result: 2
4.
Code--: echo 14;
Result: 2
5.
Code--:
Result: 214
I hope that makes some sense, and remember the return of print is always 1, and any function that prints or echo's while inside another execution will echo/print before it's caller/parent does.
The 1 in between is actually a true statement.
Because print statement actually returns a true.
So you get 2 (from print), 1 (from echo print), and 4 (from 1+3)
print is executed first because of the parenthesis, so it print's 2 first, but it returns 1. Then, your echo gets executed, printing 1. You then concatenate to it the result of print (which is 1) with 3 added to it. That's 4.
print always return 1 according to: http://php.net/manual/en/function.print.php
So, because arithmetic operator has precedence over one for concatenation, so you get:
"1" . (1+3)
... that is "14" ;). And because print sends string directly to output you get '2' in front of everything....

Categories