问题
Can some one tell why my php line break not working ( echoing ) ?
I know i can write the code in a different way to make the line break work, but i want to know the reason behind this ?
<?php
$var1 = 3;
echo "Addition = " . $var1 += 3 . "<br>";
echo "Subtraction = " . $var1 -= 3 . "<br>";
echo "Multiplication = " . $var1 *= 3 . "<br>";
echo "Division = " . $var1 /= 3 . "<br>";
?>
回答1:
You can use commas,
echo "Addition = " . $var1 += 3 , "<br>";
echo "Subtraction = " . $var1 -= 3 ,"<br>";
echo "Addition = " . $var1 *= 3 , "<br>";
echo "Addition = " . $var1 /= 3 ,"<br>";
Or wrap it in brackets:
echo "Addition = " . ($var1 += 3) . "<br>";
echo "Subtraction = " . ($var1 -= 3) ."<br>";
echo "Addition = " . ($var1 *= 3) . "<br>";
echo "Addition = " . ($var1 /= 3) ."<br>";
Otherwise the 3
number is concatenated with <br>
.
回答2:
Well seems like I have to clean some things up here.
Let's take a look at the operator precedence, which says:
.
has a higher precedence, than+=
,-=
,*=
,/=
.
is left associative=
,+=
,-=
,*=
,/=
is right associativeWe also take a look at the note at the bottom of the manual:
Note: Although = has a lower precedence than most other operators, PHP will still allow expressions similar to the following: if (!$a = foo()), in which case the return value of foo() is put into $a.
Means that even tough =
has a lower precedence than .
it gets evaluated first. You can also see this if you do something like this:
$xy = "HERE";
echo "I am " . $xy = "NOT HERE";
Now you would think that .
has a higher precedence than =
and will get evaluated first, but as from the note in the manual, the assignment is first and you end up with this:
echo "I am " . ($xy = "NOT HERE");
output:
I am NOT HERE
So if we put all these information's together, we can say, that the assignment gets evaluated first, but it's right assocative. Means this:
$var1 = 3;
echo "Addition = " . ($var1 += 3 . "<br>");
echo "Subtraction = " . ($var1 -= 3 . "<br>");
echo "Addition = " . ($var1 *= 3 . "<br>");
echo "Addition = " . ($var1 /= 3 . "<br>");
So this code will end up in this:
echo "Addition = " . ($var1 += "3<br>");
echo "Subtraction = " . ($var1 -= "3<br>");
echo "Addition = " . ($var1 *= "3<br>");
echo "Addition = " . ($var1 /= "3<br>");
Which then through the arithmetic operator gets convert to an integer we end up with this:
echo "Addition = " . ($var1 += 3);
echo "Subtraction = " . ($var1 -= 3);
echo "Addition = " . ($var1 *= 3);
echo "Addition = " . ($var1 /= 3);
And after the assignment is done the concatenation gets evaluated, which looks like this:
echo "Addition = " . 6;
echo "Subtraction = " . 3;
echo "Addition = " . 9;
echo "Addition = " . 3;
With this you end up in this output:
Addition = 6Subtraction = 3Addition = 9Addition = 3
And now how to solve this? Simply wrap your assignment in parentheses, so that the <br>
tag doesn't get into the assignment. E.g.
echo "Addition = " . ($var1 += 3) . "<br>";
echo "Subtraction = " . ($var1 -= 3) . "<br>";
echo "Multiplication = " . ($var1 *= 3) . "<br>";
echo "Division = " . ($var1 /= 3) . "<br>";
//^ ^ So the br tag doesn't get in the assignment of the variable.
回答3:
This is happening because of the type casting issues. 3 . "<br>"
will be converted to number while the operation will be performed. Wrap the inside ()
so that the operations are performed first then the concatenation.
echo "Addition = " . ($var1 += 3) . "<br>";
echo "Subtraction = " . ($var1 -= 3) ."<br>";
echo "Addition = " . ($var1 *= 3) . "<br>";
echo "Addition = " . ($var1 /= 3) ."<br>";
回答4:
Your PHP means:
echo "Addition = " . $var1 += (3 . "<br>");
echo "Subtraction = " . $var1 -= (3 ."<br>");
echo "Addition = " . $var1 *= (3 . "<br>");
echo "Addition = " . $var1 /= (3 ."<br>");
And number + 3 . '<br>'
is number + (int)(3 . '<br>')
which is number + 3
. No <br>
exists now due to retyping to number(converting to number).
Use brackets around equations.
echo "Addition = " . ($var1 += 3) . "<br>";
echo "Subtraction = " . ($var1 -= 3) ."<br>";
echo "Addition = " . ($var1 *= 3) . "<br>";
echo "Addition = " . ($var1 /= 3) ."<br>";
回答5:
Try this..
"." is used for php variable to concate not for numbers
<?php
$var1 = 3;
echo "Addition = ". ($var1 += 3) ."</br>";
echo "Subtraction = ". ($var1 -= 3) ."</br>";
echo "Addition = ". ($var1 *= 3) ."</br>";
echo "Addition = ". ($var1 /= 3) ."</br>";
?>
回答6:
Try this way.
<?php
$var1 = 3;
echo "Addition =" . ($var1 += 3 ).'<br>';
echo "Subtraction =" . ($var1 -= 3).'<br>';
echo "Addition =" . ($var1 *= 3 ).'<br>';
echo "Addition =" . ($var1 /= 3 ).'<br>';
?>
来源:https://stackoverflow.com/questions/30435441/why-doesnt-the-html-br-break-line-tag-doesnt-work-in-this-code