Jun 24 2009

Difference between parsing variable and parsing reference

I found an interesting code when I’m coding.

what  is difference between :

$_fx  = &$a ;

with

$_fx  = $a ;

The first one,  we call it parsing reference, and the second one , is parsing value.

you can see the difference with this sample :

<?</code></code>

<code><code> </code>

<code>echo "without reference: <BR>";
$a = 5;
$b = $a;

echo " a : 5<BR>";
echo '$b = $a <BR>';
echo "${b} = ${a} <BR>";

for($i=1;$i<5;$i++){
$b++;
echo "a(${a}) | b(${b}) <BR>";
}

echo "<BR><BR>";

echo "with reference (&amp;amp;amp;) : <br>";

$a = 5;
$b = &amp;amp;amp;$a;

echo " a : 5<BR>";
echo '$b = &amp;amp;amp;$a <BR>';
echo "${b} = ${a} <BR>";

for($i=1;$i<5;$i++){
$b++;
echo "a(${a}) | b(${b}) <BR>";
}

?>

</code>

</code>

<code><code>

as you can see, using ‘&’ , the value of $a is also changed .

so what I get is , if you using a reference, any changes you made on the reference variable ($b) will efect the real variable ($a)

3 Comments

  • By polutan, April 19, 2010 @ 2:03 am

    maksud anda passing by reference? i think it not about parsing… yeaa that’s a essensial technique in programming with C like language.

  • By leonardo, April 19, 2010 @ 5:49 pm

    koreksi, mas.
    bukan ‘parsing’ tapi ‘passing’ (oper).

    salam kenal :)

  • By dedy, May 10, 2010 @ 9:34 pm

    oops , my mistake :)

Other Links to this Post

RSS feed for comments on this post. TrackBack URI

Leave a comment

WordPress Themes