Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php: what's the difference between $var and &$var?

Tags:

What is the difference between

foreach ($my_array as $my_value) { } 

And:

foreach ($my_array as &$my_value) { } 

?

May I ask you to give me two real-world examples of when to use one and when you use the other?

like image 526
Olivier Pons Avatar asked Dec 29 '10 15:12

Olivier Pons


People also ask

What is the difference between $name and $$ name?

I did some research and found out that $$name is a reference variable, and $name is just a variable.

What is the difference between if (' value == $var and if ($ var == value ') in PHP?

The difference is that the isset will be true for any variable that has been declared/initalized to anything (besides null). the if($var) is true for all values of var OTHER THAN those that evaluate to false (0, '', null, false, etc).

What is the difference between $message and $$ message?

$message is used to store variable data. $$message can be used to store variable of a variable. Data stored in $message is fixed while data stored in $$message can be changed dynamically.

What is the difference between == and === operator in PHP?

== Operator: This operator is used to check the given values are equal or not. If yes, it returns true, otherwise it returns false. === Operator: This operator is used to check the given values and its data type are equal or not. If yes, then it returns true, otherwise it returns false.


2 Answers

The first example creates a copy of the value, whereas the second uses a reference to the original value. So after the first foreach runs, the original array is still untouched. After the second foreach the original array could have been modified since it was handled by reference.

Some native PHP functions already work this way, such as shuffle() which rearranges the contents of your array. You'll notice that this function doesn't return an array, you just call it:

$myArray = array('foo', 'bar', 'fizz', 'buzz'); shuffle( $myArray ); // $myArray is now shuffled 

And it works its magic since it works with the array by reference rather than creating a copy of it.

Then there are functions that don't pass anything by reference but rather deal with a copy of the original value, such as ucwords() which returns the new resulting string:

$myString = "hello world"; $myString = ucwords( $myString ); // $myString is now capitalized 

See Passing by Reference.

like image 73
Sampson Avatar answered Oct 26 '22 13:10

Sampson


Jonathan's answers describes it very well. Just for completeness, here are your two examples:

  1. Just reading values:

    $my_array = range(0,3); foreach ($my_array as $my_value) {     echo $my_value . PHP_EOL; } 
  2. Adding some number to each element (thus modifying each value):

    foreach ($my_array as &$my_value) {     $my_value += 42; } 

    If you don't use &$my_value, then the addition won't have any effect on $my_array. But you could write the same not using references:

    foreach($my_array as $key=>$value) {     $my_array[$key] = $value + 42; } 

    The difference is that we are accessing/changing the original value directly with $my_array[$key].

DEMO

like image 39
Felix Kling Avatar answered Oct 26 '22 14:10

Felix Kling