I have published the following problem on the “Developers Zone” board of my mobile community site, JOTIL21.net. I am sorry, none could answer the question. They all tried to make hypothesis instead of practically trying out the script. So, I am posting the solution here.
Question:
What’s the output of the following code? Explain the code.
<?php $a = "1"; $b = &$a; $b = "2$a"; echo $a; ?>
I am just shocked! If anybody just copy-pasted the code and tested out, they would have known the output is “21″.
Here’s the code explanations:
<?php $a = "1"; // assign 1 to $a $b = &$a; // $b references to $a $b = "2$a"; // $b changes to "2$a" that is "21" since $a == 1 echo $a; // since $b is a reference to $a, $a == $b ?>
It couldn’t have been easier… Shame on you guys…
Nah re vai, poora khele dilen
PHPPrince is now PHPNobish! Back to the drawing board…..