2

I am using PHP 5.6.8 Version in xampp server. In that i am getting error in output in output the addition and sub time i got this output for this code

    <? 
echo"Hello PHP"."&nbsp;". "Whats up"."<br/>";
echo "ADDING". 2+2 ."<br/>";
echo "SUB".3-2 ."<br/>";
echo "MUL". 2*2 ."<br/>";
echo "DIV". 2/2 ."<br/>";
echo "MODULS". 5%2 ."<br/>";
?>

Output

Hello PHP Whats up
2
-2
MUL4
DIV1
MODULS1
devajay
  • 399
  • 4
  • 12
  • Welcome to PHP [type juggling](https://php.net/manual/ro/language.types.type-juggling.php). You need to specifically delimit mathematical operation from concatenation(it's good practice to delimit them generally). Since concatenation has, stupidly enough, a higher priority than some mathematical operations. Long story short: `echo "ADDING". (2+2) ."
    ";` - notice the brackets.
    – Andrei Jun 03 '15 at 10:44

2 Answers2

2

Try using ,(comma) instead of .(dot). As its all because of operator precedence and for how precedence operator work there's a answer within Why doesn't the html br break line tag doesn't work in this code? answered by Rizier123

echo"Hello PHP"."&nbsp;". "Whats up"."<br/>";
echo "ADDING", 2+2 ,"<br/>";
echo "SUB", 3-2 ,"<br/>";
echo "MUL", 2*2 ,"<br/>";
echo "DIV", 2/2 ,"<br/>";
echo "MODULS", 5%2 ,"<br/>";
Community
  • 1
  • 1
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
0

2+2 ."<br/>" will be treated as 2 + (2 . "<br/>") which is not correct.

Add () -

echo "ADDING". (2+2) ."<br/>";
echo "SUB". (3-2) ."<br/>";
echo "MUL". (2*2) ."<br/>";
echo "DIV". (2/2) ."<br/>";
echo "MODULS". (5%2) ."<br/>";

There is no space between . and 3 on echo "SUB".3-2 ."<br/>";. That is the reason it is giving error for that line and others are working.

Sougata Bose
  • 31,517
  • 8
  • 49
  • 87