This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Reference - What does this symbol mean in PHP?
what do “=&” / “&=” operators in php mean?
Sorry guys I feel like I'm asking too simple of a question but what is =& in PHP? I tried to use this group function with ACL in Cakephp...
You use =& when you want to assign a variable by reference. For more information see http://php.net/manual/en/language.references.php.
Example:
$a = array(1, 2, 3);
// $b is a reference to $a.
// If you change $a or $b, the value for both $a and $b will be changed.
$b =& $a;
$c = array(1, 2, 3);
// $d is a copy of $c.
// If you change $d, $c remains unchanged.
$d = $c;
$b = 3;
$a =& $b;
$b = 5; //$a is 5 now
$a = 7; //$b is 7 now
Related
This question already has answers here:
Are there pointers in php?
(9 answers)
Closed 7 years ago.
In C we can use pointer for a function parameter:
test( *text);
function test( *txt){
//codes goes here
}
Is it possible in PHP?
Variable names in PHP start with $ so $entryId is the name of a variable.
$this is a special variable in Object Oriented programming in PHP, which is reference to current object.
-> is used to access an object member (like properties or methods) in PHP, like the syntax in C++.
So your code means this: place the value of variable $entryId into the entryId field (or property) of this object.
The & operator in PHP, means pass reference. Here is an example:
$b=2;
$a=$b;
$a=3;
print $a;
print $b;
// output is 32
$b=2;
$a=&$b; // note the & operator
$a=3;
print $a;
print $b;
// output is 33
In the above code, because we used & operator, a reference to where $b is pointing is stored in $a. So $a is actually a reference to $b.
There is a good explanation of pointers on this page
There are references, but that's not the same as pointers.
php.net has multiple pages explaining What References Are, What References Do and What References Are Not.
There too, it is mentioned multiple times that
References are not pointers.
They provide a way to assign $a to $b so that if you reassign $b, $a changes as well:
$a = 'a';
$b = &$a; // Reference
$b = 'b'; // Now $a == 'b'
This can be used for function arguments as well:
function myFunc(&$b)
{
$b = 'b';
}
$a = 'a';
myFunc($a); // Now $a == 'b'
Before PHP 5.3.0 is was also possible to do a thing called "call-time pass-by-reference", where you would have a "normal" function declaration and use the & operator in the call instead:
function myFunc($b)
{
$b = 'b';
}
$a = 'a';
myFunc(&$a); // As of PHP 5.3.0 produces a Fatal Error, saying:
// Call-time pass-by-reference has been removed; If you would like to pass argument by reference, modify the declaration of myFunc().
But beware! Assigning another reference will not update the original reference:
$a = 'a';
$b = 'b';
$c = &$a;
$c = &$b; // $a == 'a'
[ Demo ]
A trap resulting from that exists with the global keyword.
$a = 'a';
$b = 'b';
function myFunc()
{
global $a, $b;
$a = &$b;
var_dump($a);
}
myFunc(); // 'b'
var_dump($a); // 'a'
That is because global $a effectively means $a = &$GLOBALS['a'], so assigning it a new reference will not update $GLOBALS.
Of course you can prevent that by using $GLOBALS directly.
But if you're using globals at all, you should probably rethink your design anyway.
With references, there is now also a difference between setting a variable = NULL and using unset().
= NULL follows references, unset() does not:
$a = 'a';
$b = &$a;
unset($b); // $a == 'a'
$a = 'a';
$b = &$a;
$b = NULL; // $a == NULL
[ Demo ]
Bottom line:
References allow for things that wouldn't be possible without them, but sometimes do not behave the way one would expect them to, because of how PHP was built.
Is
$a = 1;
$b = $a;
equal to writing this?
$a = $b = 1;
Will the second example always put 1 as value to both $a and $b, even if $a and $b already has a value assigned to them?
Quoting the documentation:
The value of an assignment expression is the value assigned. That is,
the value of "$a = 3" is 3. This allows you to do some tricky things:
<?php
$a = ($b = 4) + 5; // $a is equal to 9 now, and $b has been set to 4.
?>
So, to answer your question, the result of the assignment $b = 1 is 1, and therefore, $a = $b = 1 would assign the value of $b = 1--which is to say 1--to $a.
That being said, abusing this can lead to code that is hard to read.
Yes, PHP will put 1 in $b then put $b value in $a, i.e. 1.
There is no ambiguity as the first assignment is $b = 1, the next is $a = $b.
I wonder if there is any way can short sentence to state a variable.
Purpose: only for if you are in a situation have to state 20 variables at the time. more convenience
$a = 1; $b = 2;
//Imagination like below
$a, $b = 1, 2;
$a = 1, $b = 2;
Thank you very much for your advice of alternatives.
(If you do not have any ideas, please do not accuse the way of why have to think about this), because arrray, (object) are alternatives, but not match what I need on my question
The closest you can get to that syntax is using list():
<?php
list ($a, $b) = array(1, 2);
echo $a . ' ' . $b; // prints "1 2"
Its magic!
EDIT:
For even more magic, you can use short array notation from PHP 5.4 onwards:
<?php
list($a, $b) = [1, 2];
print $a . ', ' . $b; // prints 1, 2
How is $a = 1, $b = 2; shorter than $a = 1; $b = 2; ?
If for any reason you need two assignments in one statement, you could to it with list:
list($a,$b) = array(1,2);
However, shortening is not a valid reason for this.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Coalesce function for PHP?
I'm not sure what this is normally called, but I hope the title communicates well enough. What I have is a handful of variables some of which might be null.
I want to do:
$a = $b || $c || $d;
Where $a ends up being = to the first non-null variable.
To my knowledge, PHP doesn't support this in the same way JavaScript does.
You can, however do something like this:
$a = $b ? $b : ($c ? $c : $d);
A more general solution:
function fallthrough($arr) {
//$arr should be an array of possible values. The first non-null value is returned
do $a = array_shift($arr);
while($a === null && $arr);
return $a;
}
<?php
$a = 0;
$b = false;
$c = true; //should become this
$d = '1';
$e = $a ?: $b ?: $c ?: $d;
var_dump($e);
//bool(true)
//should be '1' if order is different
$e = $a ?: $b ?: $d ?: $c;
var_dump($e);
//string(1) "1"
... however ?: is kinda new, you will confuse your colleagues / fellow coders.
I don't think that's possible. I think you'd have to use some other, more laborious, way. I.e. make an array of the variables, iterate through it until you find a non-null value and break the loop, like so:
$vars = array("b" => $b, "c" => $c, "d" => $d);
foreach($vars as $var) {
if($var != null) {
$a = $var;
break;
}
}
Well, like some other answers here say, you can use the shorthand way of writing this, but writing readable code is important too. The above code is pretty readable.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What do the "=&" and "&=" operators in PHP mean?
I found the operator "=&" in the following code, and I do not know what it means. What does it mean and what does it do?
The code where I read it:
function ContentParseRoute($segments)
{
$vars = array();
//Get the active menu item
$menu =& JSite::getMenu();
$item =& $menu->getActive();
// Count route segments
$count = count($segments);
....
This isn't an assignment (=) by reference (&).
If you were to say:
$a = 42;
$b =& $a;
You are actually saying assign $a by reference to $b.
What assigning by reference does is "tie" the two variables together. Now, if you were to modify $a later on, $b would change with it.
For example:
$a = 42;
$b =& $a;
//later
echo $a; // 42
echo $b; // 42
$a = 13;
echo $a; // 13
echo $b; // 13
EDIT:
As Artefacto points out in the comments, $a =& $b is not the same as $a = (&$b).
This is because while the & operator means make a reference out of something, the = operator does assign-by-value, so the expression $a = (&$b) means make a temporary reference to $b, then assign the value of that temporary to $a, which is not assign-by-reference.
It is the referential assignment operator.
This means that when you modify the LHS of the operator later on in code, it will modify the RHS. You are pointing the LHS to the same block of memory that the RHS occupies.
Here's an example of it in use:
$array = array('apple', 'orange', 'banana');
// Without &
foreach($array as $d)
{
$d = 'fruit';
}
echo implode(', ', $array); // apple, orange, banana
// With &
foreach($array as &$d)
{
$d = 'fruit';
}
echo implode(', ', $array); // fruit, fruit, fruit
Not an explanation, but an example of being able to use the & operator without using it in an =& assignment.