What is the function of "&" right before the session variable? [duplicate] - php

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 6 years ago.
$cart = &$_SESSION['journal_items']; anybody know when do we use the & symbol like that?

Basically it means that $cart won't get the value stored in $_SESSION['journal_items'], but it's reference. Whenever you call $cart, you'll be calling the current value of $_SESSION['journal_items'], even if it has been changed after this declaration.
Example:
$_SESSION['journal_items'] = "test";
$cart = &$_SESSION['journal_items'];
//$cart's current value is "test"
$_SESSION['journal_items'] = "test2";
//$cart's current value is "test2"
See this answer and this reference book.

Related

Is declaring a PHP var valid with this syntax: &$var? [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 1 year ago.
I'm upgrading old in-house PHP code for my company and I stumbled upon this piece of code:
foreach ($array as $key => &$value) {
// do something
}
what's the meaning of &$value and can I safely assume this is a mistype?
It is not mistype. It is reference and it means to access the same variable content by different names. If you modify the value of $value, then you would also modify the original value inside $array.
You can read more about it at References Explained, Php.net

What does foo[] mean in $this->foo[] in php [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 6 years ago.
Is foo[] an array?
In the following code is an array element being assigned to foo[]? And is there an array key that is automatically associated with it?
$this->foo[] = 'hello world';
This is adding an element to the end of the foo array.
It's the same as using array_push().

Meaning of extra $ variable in PHP [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 7 years ago.
I want to know about meaning of $$val; what is the actual meaning is?i tried to find meaning of this in google but not understand properly. Please help me in this situations.
For example: suppose i have one variable which has $$value;
meaning of $$value?
You didn't put the language, but I'll assume you mean PHP
That's a variable variable.
That means you ware asking for the value of the variable whose name.is the first variable.
Here's an example, since that's quite confusing:
$foo = "Hi";
$bar = "world";
$world = "Hello!";
echo $$bar; // "Hello!"
php fiddle: http://ideone.com/Ve4YOO
Reference: https://secure.php.net/manual/en/language.variables.variable.php

Ambigous PHP error message [duplicate]

This question already has answers here:
Only variables should be passed by reference
(12 answers)
Closed 7 years ago.
Having this:
...
private $responseBuffer = array();
...
and within this line:
$lm = end(array_values($this->responseBuffer));
I get
Error: Only variables should be passed by reference (2048)
as both end and array_values are built in and do not have a call-by-referenceI'm puzzled, any one an idea?
(purpose: get the latest value out of $responseBuffer)
end function receives the arguement by reference, do like this:
$var = array_values($this->responseBuffer);
$lm = end($var);

php variable from variable [duplicate]

This question already has answers here:
Mixing a PHP variable with a string literal
(5 answers)
Closed 9 years ago.
I have a variable and I want to create a variable with that. I get the variable from database and put it together with some text and then I want another variable.
For exampel
$a = $ . "txt" . $d;
Try with this. It will create a variable from another one.
$a = ${'txt'.$d}
P.s. This is a question asked a couple of times. You might have found the answer simply by searching the issue on google.

Categories