What will be output of the below code?
<?php
$a=array(1,2,3);
$b=array(4,5,6);
$c=1;
$a[$c++]=$b[$c++];
print_r($a);
?>
a) Array(1,4,3)
b) Array(1,6,3)
c) Array(4,2,6)
d) Array(1,5,3)
e) Array(1,2,5)
Solution: b) Array(1,6,3)
Now take some time and think how we got this solution!
At first we assume that right side of = is calculated first and is then assigned to left side.
So $b[$c++] should return 5, by then $c would have reached to 2 so $a[2]=5;
and output should be Array(1,2,5)
But what actually happens is that left side of = is calculated first, and then the right B-)
<?php
$a=array(1,2,3);
$b=array(4,5,6);
$c=1;
$a[$c++]=$b[$c++];
print_r($a);
?>
a) Array(1,4,3)
b) Array(1,6,3)
c) Array(4,2,6)
d) Array(1,5,3)
e) Array(1,2,5)
Solution: b) Array(1,6,3)
Now take some time and think how we got this solution!
At first we assume that right side of = is calculated first and is then assigned to left side.
So $b[$c++] should return 5, by then $c would have reached to 2 so $a[2]=5;
and output should be Array(1,2,5)
But what actually happens is that left side of = is calculated first, and then the right B-)
No comments:
Post a Comment