What Does the "&" Sign Mean in PHP

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

Reference Guide: What does this symbol mean in PHP? (PHP Syntax)

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

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 does & sign mean in front of a variable?

It means pass the variable by reference, rather than passing the value of the variable. This means any changes to that parameter in the preparse_tags function remain when the program flow returns to the calling code.

function passByReference(&$test) {
$test = "Changed!";
}

function passByValue($test) {
$test = "a change here will not affect the original variable";
}

$test = 'Unchanged';
echo $test . PHP_EOL;

passByValue($test);
echo $test . PHP_EOL;

passByReference($test);
echo $test . PHP_EOL;

Output:

Unchanged

Unchanged

Changed!

PHP &$string - What does this mean?

You are assigning that array value by reference.

passing argument through reference (&$) and by $ is that when you pass argument through reference you work on original variable, means if you change it inside your function it's going to be changed outside of it as well, if you pass argument as a copy, function creates copy instance of this variable, and work on this copy, so if you change it in the function it won't be changed outside of it

Ref: http://www.php.net/manual/en/language.references.pass.php

How does the & operator work in a PHP function?

The & operator tells PHP not to copy the array when passing it to the function. Instead, a reference to the array is passed into the function, thus the function modifies the original array instead of a copy.

Just look at this minimal example:

<?php
function foo($a) { $a++; }
function bar(&$a) { $a++; }

$x = 1;
foo($x);
echo "$x\n";
bar($x);
echo "$x\n";
?>

Here, the output is:

1
2

– the call to foo didn’t modify $x. The call to bar, on the other hand, did.

What does a single ampersand mean in a PHP conditional statement?

That is a bitwise AND operation: http://www.php.net/manual/en/language.operators.bitwise.php

If, after the bitwise AND, the result is "truthy", the clause will be satisfied.

For example:

3 & 2 == 2 // because, in base 2, 3 is 011 and 2 is 010
4 & 1 == 0 // because, in base 2, 4 is 100 and 1 is 001

This is commonly used to check a single bit in a bitset, by testing powers of two, you are actually checking if a specific bit is set.

Difference between & and && in PHP

& is bitwise AND. See Bitwise Operators. Assuming you do 14 & 7:

    14 = 1110
7 = 0111
---------
14 & 7 = 0110 = 6

&& is logical AND. See Logical Operators. Consider this truth table:

 $a     $b     $a && $b
false false false
false true false
true false false
true true true

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.



Related Topics



Leave a reply



Submit