March 23, 2011

difference when using & and not using &

Question by Hacker

Possible Duplicate:
Reference – What does this symbol mean in PHP?

Hi,
I was using like

$formCheckbox = $form->addElement('radio',...);
$formCheckbox->setChecked(true);

this was working on windows properly but was not on linux i.e it was not checking the radio button.

so i changed it like

$formCheckbox = **&**$form->addElement('radio',...);
$formCheckbox->setChecked(true);

so i just used & while creating the element. I just wanted to know how does it make a difference. I am using HTML quick forms.

Answer by sharpner

in this case there is not much of a difference, because php handles objects internally as pointers..

but as long as you do not know what & stands for, don’t use it..

a short introduction to pointer:

$a = 10; 
$b = &$a; 
$b = 20; 
echo $a; -> 20 

$a = 10; 
$b = $a; 
$b = 20; 
echo $a; -> 10

so with & you only reference to another variable, instead of creating a new one

Answer by Starx

It is passing the variable as a reference, so that the changes are maintained always.

Check this http://php.net/manual/en/language.references.pass.php

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!