Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between “=” and “=&” when assigning variables?

I am trying to figure out the difference between $a=&$b and $a=$b. I know & make the variable to be a reference variable. But the following test gave me the same result. Can anyone explain the difference? Thanks.

 $a=5;
 $b=6;

 $a=&$b;
 echo $a; //6


 $a=5;
 $b=6;

 $a=$b;
 echo $a; //6
like image 456
FlyingCat Avatar asked Jul 24 '10 17:07

FlyingCat


People also ask

What's the difference between single and double quotation marks?

General Usage Rules In America, Canada, Australia and New Zealand, the general rule is that double quotes are used to denote direct speech. Single quotes are used to enclose a quote within a quote, a quote within a headline, or a title within a quote.

What is the difference between single and double inverted commas?

Double quotation marks (in British English) are used to indicate direct speech within direct speech (use single inverted commas for direct speech and double quotation marks to enclose quoted material within).

What is the difference between inverted commas and quotation marks?

Inverted commas are mainly used to introduce quotations or direct speech. This is why the name quotation mark is used as a synonym for inverted commas. A direct quote (the exact words spoken or written) must be always written inside quotation marks.

What is the difference between and and and?

In general, using & implies a much more informal tone than and. You will never be criticized for using and, whereas you run the risk of disapproval if you use & in anything but informal notes, tweets and the like.


1 Answers

First of all: you'll hardly ever need references, avoid the confusion of using them if you can.

$a=5;    //assign value to a
$b=&$a;  //make $b a reference to $a
$b=6;    //assigning a value to $b assigns the same value to $a (as they point to the same location
echo $a; //6


$a=5;    //assign a value to a
$b=$a;   //set $b to the value of $a
$b=6;    //set $b to another value leaves $a at it's original value
echo $a; //5
like image 87
Wrikken Avatar answered Oct 31 '22 04:10

Wrikken



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!