What does a &
in front of a variable name mean?
For example &$salary
vs. $salary
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 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).
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.
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' ?>
It's a reference, much like in other languages such as C++. There's a section in the documentation about it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With