$variable after <br> is not showing in echo in php - 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

Related

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;
?>

PHP echo error - same echoes, few work, few doesn't

I have a really weird problem: Below in the code listed 4 echo's are pretty much the same, but only the last ones work properly (First two echoes only print the answer of addition/ subtraction, no text).
Here's the code:
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?><br>
<?php $sk1 = $_POST["sk1"];
$sk2 = $_POST["sk2"];
$veiksm = $_POST["veiksmas"];
switch($veiksm){
default:
echo "Jus nepasirinkote veiksmo";
break;
case "sud":
echo "Sudeties veiksmo rezultatas: " .$sk1 + $sk2;
break;
case "ati":
echo "Atimties veiksmo rezultatas: " .$sk1 - $sk2;
break;
case "dal":
echo "Dalybos veiksmo rezultatas: " .$sk1 / $sk2;
break;
case "dau":
echo "Daugybos veiksmo rezultatas: " .$sk1 * $sk2;
break;
}
?>
</body>
</html>
You have to put brackets around your calculation like:
echo "Atimties veiksmo rezultatas: " . ($sk1 - $sk2);
//^ See here ^
Otherwise you can imagine your echo statement like this:
"Sudeties veiksmo rezultatas: 17" + 5 // Same as 0 + 5, because the string is casted to int which is 0
Also * and / works because they are getting evaluated first
Put parentheses around the calculation, like this:
echo "Sudeties veiksmo rezultatas: " . ($sk1 + $sk2);
The reason for this is the order in which the expression is processed. Without parentheses, PHP evaluates everything from left to right, so it will first concatinate $sk1 to the string. The combined value will be "Sudeties veiksmo rezultatas: 1" (if $sk1 is 1).
After that, $sk2 is added to that value. Because PHP cannot add up a string, it tries to convert it to a number. This conversion fails, because the string starts with a non-numeric text, and defaults to 0 which is added to the value of $sk2.
Multiplication and division operators have higher precedence, so they are evaluated first, overriding the left-to-right order. That's why it works for the last two cases.

A string in PHP that doesn't make sense

I was experimenting with weak/dynamic typing properties of PHP in preparation for a test and was completely baffled by the output of this string concatenation. Can someone explain how this is even possible?
<?php echo 1 . "/n" . '1' + 1 ?><br />
output:
2
Analysis:
echo 1 . "/n" . '1' + 1;
is equivalent to
//joined first 3 items as string
echo "1/n1"+1;
is equivalent to
//php faces '+' operator, it parses '1/n1' as number
//it stops parsing at '/n' because a number doesn't
//contain this character
echo "1"+1;
is equivalent to
echo 1+1;

Simple arithmetic in PHP

Here is a simple php program which gives a strange output. Can anyone explain why it is coming like this and how to get the expected output?
<?php
$a=2;$b=3;
echo "<br> ADD:".$a+$b;
echo "<br> SUB:".$a-$b;
echo "<br> MUL:".$a*$b;
echo "<br> DIV:".$a/$b;
?>
Output:
3-3
MUL:6
DIV:0.66666666666667
Expected Output:
ADD:5
SUB:-1
MUL:6
DIV:0.66666666666667
It is because the string concatenation operator . has the same precedence as the add/sub operators, and all of them are left-associative. This means that evaluation proceeds left-to-right, so "<br> ADD:".$a is evaluated first and the result is added to 3. This particular string converts to zero and 0 + 3 = 3. Similar for the subtraction.
Solution: put the arithmetic in parentheses.
echo "<br> ADD:".($a+$b);
echo "<br> SUB:".($a-$b);
On the other hand, mul/div have higher precedence than concatenation so they produce the expected result.

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