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.
Related
I have the code below. I want to change $b to use it again with values. If I do so it changes $a as well. How can I assign a value to $b again after previously assigning it as a reference to $a?
$a = 1;
$b = &$a;
// later
$b = null;
See explanation inline
$a = 1; // Initialize it
$b = &$a; // Now $b and $a becomes same variable with
// just 2 different names
unset($b); // $b name is gone, vanished from the context.
// But $a is still available
$b = 2; // Now $b is just like a new variable with a new value.
// Starting a new life.
$a = 1;
$b = &$a;
unset($b);
// later
$b = null;
The answer by #xdazz is correct, but just to add the following great example from the PHP Manual which gives an insight into what PHP is doing under the hood.
In this example you can see that $bar within the function foo() is a static reference to a function scope variable.
Unsetting $bar removes the reference but doesn't deallocate the memory:
<?php
function foo()
{
static $bar;
$bar++;
echo "Before unset: $bar, ";
unset($bar);
$bar = 23;
echo "after unset: $bar\n";
}
foo();
foo();
foo();
?>
The above example will output:
Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23
First of all: Creating a reference from $a to $b creates a connection (for the lack of a better word) between the two variables, so $a changing when $b changes is exactly the way it is meant to work.
So, assuming you want to break the reference, the easiest way ist
unset($b);
$b="new value";
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.
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 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
I have two variables in PHP, say $a and $b. $a is a string variable. It contains $b. I want to update $a automatically if $b is updated.
$b = 4;
$a = "value is ".$b;
echo $a; // value is 4
$b = 5;
echo $a; // should print value is 5
Yes, $a can be updated automatically if you assign $b to $a by reference, but there should not be any string concatenation assigned to $a.
Try:
$b = 4;
$a = &$b;
$c = 'Value is ';
echo $c.$a;
$b = 5;
echo $c.$a;
Here is a demo
Not possible the way you want it. You see, variables can be passed by reference, like so:
$a = &$b;
Which will cause $a to automatically update when $b changes, however, it may not contain any other value, (like the string you want), so you'll have to use a function or another variable to do it.
$b = &$a;
echo "Value is $b";
or
$b = &$a;
$description = "Value is ";
echo $description . $b;
PHP doesn't have that feature. Related features you could use are:
References, which let you alias one variable to another. The value of each variable is the same, since they're simply symbol table aliases.
$b = "I'm b."
$a =& $b;
echo $a;
Variable variables, in which one variable holds the name of the other.
$b = "I'm b."
$a = 'b';
echo $$a;
However, variable variables should generally be avoided as they generally cause needless obfuscation.
Functions (as mithunsatheesh suggests). This is closest to what you want, as a function call is an expression that will have the value you're looking for. The only place a function wouldn't work where a variable would is when interpolating the value into a double-quoted string or a heredoc. Instead, you'd have to use string concatenation, or assign the result of the function call to a local variable and interpolate that.
You should pass it by reference. How to do it ?
Make a function:
function showValue(&$b)
{
return 'value is ' . $b;
}
echo showValue($b);
I think this should work.
Take a look at http://www.php.net/manual/en/language.references.whatdo.php
$a = 4;
$b =& $a;
$a = 5;
echo $b; // should print 5;
When a php script runs it runs "line after line". When you assign like this
$b = 4;
$a = "value is ".$b;
Value of $b is already assigned to $a as a integer 4 (not $b). So, if next $b is updated to some other value. Variable $a has no idea about it.
In this kind of case you have to use function or variable reference as describe in some other answers
$a = 4;
$b =& $a;
$a = 5;
echo $b;