Here is the code:
<?php
for($i =0, $x = 100 ; $i<1; $i++){
echo $x . 'y' . $i+1 . ' = '. $i*$x . ' <br>';
}
?>
My Expected Output was: 100 y 1 = 0
But the actual result was: 101 = 0;
Where did 'y' go?
Here is the code:
<?php
for($i =0, $x = 100 ; $i<1; $i++){
echo $x . 'y' . $i+1 . ' = '. $i*$x . ' <br>';
}
?>
My Expected Output was: 100 y 1 = 0
But the actual result was: 101 = 0;
Where did 'y' go?
.
have more operator precedence than +
.
echo $x . 'y' . $i+1 = 101
Because it will operate as
echo ($x . "y" . $i)+1 ;
This is what happens.
$x3= ($x . "y" . $i); //100y0
$u = $x3+1 ; //101
You are doing +
operation on a string. So the first digits before any characters will be taken as integer value.
Eg:
10y0g8 = 10
t10 =0
By doing an Arithmetic Operation, interpreter will convert string to integer, and it will discard all other characters
. so 100y+1 = 101
it won't be 101y
as explained in the above (Subin Thomas) answer the addition of 1 with the varchar value like 100y0 will add 1 with 100(the first occurence of integer). the following code will work as you expected
<?php
for($i =0 ,$x = 100; $i<1; $i++){
echo $x . 'y' . ($i+1) . ' = '. $i*$x . ' <br>';
}
?>
To test :
<?php
for($i =0, $x = 100 ; $i<1; $i++){
echo $x . 'y' . ($i+1) . ' = '. $i*$x . ' <br>';
}
?>