1

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

?>
Rizier123
  • 56,111
  • 16
  • 85
  • 130
suren
  • 46
  • 1
  • 10

6 Answers6

8

Well seems like I have to clean some things up here.

Let's take a look at the operator precedence, which says:

  1. . has a higher precedence, than +=, -=, *=, /=

  2. . is left associative

  3. =, +=, -=, *=, /= is right associative

  4. We 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.
Community
  • 1
  • 1
Rizier123
  • 56,111
  • 16
  • 85
  • 130
4

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>";
Sougata Bose
  • 30,169
  • 8
  • 42
  • 82
3

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

n-dru
  • 9,039
  • 2
  • 25
  • 39
3

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>";
Sougata Bose
  • 30,169
  • 8
  • 42
  • 82
pavel
  • 24,015
  • 8
  • 38
  • 57
  • 3
    The concatenation dot is left associative! NOT right! – Rizier123 May 25 '15 at 09:53
  • @Rizier123: are you sure? It works me well as I wrote above. Or... is there another reason why the behavior is as I wrote? You can insert own answer if you know st. more. – pavel May 25 '15 at 09:55
  • From the [manual](http://php.net/manual/en/language.operators.precedence.php) : **left** `* / % arithmetic` – Rizier123 May 25 '15 at 09:56
1

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

?>
Deenadhayalan Manoharan
  • 5,227
  • 13
  • 26
  • 47
1

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

?>
Mahadeva Prasad
  • 669
  • 7
  • 19