What Does =& Mean in PHP

What does =& mean in PHP?

It passes by reference. Meaning that it won't create a copy of the value passed.

See:
http://php.net/manual/en/language.references.php (See Adam's Answer)

Usually, if you pass something like this:

$a = 5;
$b = $a;
$b = 3;

echo $a; // 5
echo $b; // 3

The original variable ($a) won't be modified if you change the second variable ($b) . If you pass by reference:

$a = 5;
$b =& $a;
$b = 3;

echo $a; // 3
echo $b; // 3

The original is changed as well.

Which is useless when passing around objects, because they will be passed by reference by default.

Reference — What does this symbol mean in PHP?

Incrementing / Decrementing Operators

++ increment operator

-- decrement operator

Example    Name              Effect
---------------------------------------------------------------------
++$a Pre-increment Increments $a by one, then returns $a.
$a++ Post-increment Returns $a, then increments $a by one.
--$a Pre-decrement Decrements $a by one, then returns $a.
$a-- Post-decrement Returns $a, then decrements $a by one.

These can go before or after the variable.

If put before the variable, the increment/decrement operation is done to the variable first then the result is returned. If put after the variable, the variable is first returned, then the increment/decrement operation is done.

For example:

$apples = 10;
for ($i = 0; $i < 10; ++$i) {
echo 'I have ' . $apples-- . " apples. I just ate one.\n";
}

Live example

In the case above ++$i is used, since it is faster. $i++ would have the same results.

Pre-increment is a little bit faster because it really increments the variable and after that 'returns' the result. Post-increment creates a special variable, copies there the value of the first variable and only after the first variable is used, replaces its value with second's.

However, you must use $apples--, since first, you want to display the current number of apples, and then you want to subtract one from it.

You can also increment letters in PHP:

$i = "a";
while ($i < "c") {
echo $i++;
}

Once z is reached aa is next, and so on.

Note that character variables can be incremented but not decremented and even so only plain ASCII characters (a-z and A-Z) are supported.


Stack Overflow Posts:

  • Understanding Incrementing

What do the =& and &= operators in PHP mean?

$a &= $b is short for $a = $a & $b which is the bitwise-and operator.

$a =& $b assigns $a as a reference to $b.

What does the & sign mean in PHP?

This will force the variable to be passed by reference. Normally, a hard copy would be created for simple types. This can come handy for large strings (performance gain) or if you want to manipulate the variable without using the return statement, eg:

$a = 1;

function inc(&$input)
{
$input++;
}

inc($a);

echo $a; // 2

Objects will be passed by reference automatically.

If you like to handle a copy over to a function, use

clone $object;

Then, the original object is not altered, eg:

$a = new Obj;
$a->prop = 1;
$b = clone $a;
$b->prop = 2; // $a->prop remains at 1

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

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'
?>

What is =& in PHP?

The = and the & should* have a space between them - they're two different operators. The & means get a reference to this.

The -> is for object member access - this means assign 'xmlrpcvals' to the return_type member of $client.

* see comments for clarification

What does $this-{$key} mean in PHP?

Reads the idkey property of the $this object:

$this->idKey;

Reads the variable property name of the $this object (example in this case) so $this->example:

$idKey = 'example';
$this->$idKey;

Same as above ($this->example), but with less ambiguity (similar to adding parentheses to control operand order, and useful in some cases):

$idKey = 'example';
$this->{$idKey};

A case where this may add clarity or control the order:

$this->{$idKey['key']};
$this->{$idKey}['key'];


Related Topics



Leave a reply



Submit