PHP Problem – 03

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… :(

This entry was posted in Blog Post and tagged . Bookmark the permalink.

2 Responses to PHP Problem – 03

  1. Nah re vai, poora khele dilen :(

  2. PHPPrince is now PHPNobish! Back to the drawing board…..

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">