PHP does not echo text before + and - sign
Asked Answered
S

1

10

With the following code:

    $a=1;
    $b=1;
    echo $a."%".$b." maradéka: "." = ".$a % $b."<br>";
    echo $a."+".$b." összege: "." = ".$a + $b."<br>";

I get this output:

    1%1 maradéka: = 0
    2

As you can see, the + syntax is the same as the % but it doesn't echo the text before the operation. Maybe I'm too tired or i don't know, but i can't figure it out :D I've built dynamic web pages so far, but this one got me.

Sensualist answered 22/11, 2016 at 23:35 Comment(2)
This is actually quite interesting findTrismus
It works because the string 1+1 összege: = 1 is typecast to an int 1 and then 1+1.Adhesion
T
9

It is taking the numeric value of the first part and adding it to the second part. You'll want to group your math using parenthesis.

$a=1;
$b=1;
echo $a."%".$b." maradéka: "." = ".$a % $b."<br>";
echo $a."+".$b." összege: "." = ".($a + $b)."<br>";
Tousle answered 22/11, 2016 at 23:38 Comment(2)
To be clear, without the parentheses is the same as echo (($a."+"....$a) + $b)."<br>";, where the left hand side of the addition is a string beginning with 1, and the right hand side is int(1), hence the 2.Fan
+1 This is actually quite interesting how it processes the output. At first i did not realize that it could actually take the first value and add it to second valueTrismus

© 2022 - 2024 — McMap. All rights reserved.