Search code examples
phphtmloperator-precedence

Why doesn't the html br break line tag doesn't work in this code?


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

?>

Solution

  • 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>.