Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: What does a & in front of a variable name mean?

Tags:

php

What does a & in front of a variable name mean?

For example &$salary vs. $salary

like image 790
funk-shun Avatar asked Sep 22 '10 22:09

funk-shun


People also ask

What does this symbol mean in PHP?

This operator allows for simpler three-way comparison between left-hand and right-hand operands. The operator results in an integer expression of: 0 when both operands are equal. Less than 0 when the left-hand operand is less than the right-hand operand.

What does this -> do in PHP?

What is -> in PHP? This is referred to as the object operator, or sometimes the single arrow operator. It is an access operator used for access/call methods and properties in a PHP object in Object-Oriented Programming (OOP).

What is PHP and $$ variables?

PHP $ and $$ Variables. The $var (single dollar) is a normal variable with the name var that stores any value like string, integer, float, etc. The $$var (double dollar) is a reference variable that stores the value of the $variable inside it.


2 Answers

It passes a reference to the variable so when any variable assigned the reference is edited, the original variable is changed. They are really useful when making functions which update an existing variable. Instead of hard coding which variable is updated, you can simply pass a reference to the function instead.

Example

<?php     $number = 3;     $pointer = &$number;  // Sets $pointer to a reference to $number     echo $number."<br/>"; // Outputs  '3' and a line break     $pointer = 24;        // Sets $number to 24     echo $number;         // Outputs '24' ?> 
like image 188
Randy the Dev Avatar answered Sep 24 '22 21:09

Randy the Dev


It's a reference, much like in other languages such as C++. There's a section in the documentation about it.

like image 23
rmeador Avatar answered Sep 24 '22 21:09

rmeador