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

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

Related

Why to declare array before setting it (php)? [duplicate]

This question already has answers here:
Is it necessary to declare PHP array before adding values with []?
(13 answers)
Closed 5 years ago.
I dont know why people declare array before loops and etc..:
$new= array(); // <----- why this is needed ?
foreach($something as $v){
$new[] = $v;
}
Why to declare the array before setting its value? (In other languages, i.e. C# and JAVA it is needed, but why in PHP?)
You're not setting its value, you're pushing a new element onto the array. But there needs to be an empty array to push onto.

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

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.

PHP - Is iteration on object ordered? [duplicate]

This question already has answers here:
Is foreach guaranteed to iterate in the array order in php?
(4 answers)
How does PHP 'foreach' actually work?
(7 answers)
Closed 6 years ago.
I know that I can use this to iterate on an object:
foreach ($this as $key => $value) {
print "$key => $value\n";
}
Is the order in which the properties are looked over always the same or is it random?
I tried it a bit and it seems to always follow the declaration order, as in the exemple from here: http://php.net/manual/en/language.oop5.iterations.php
But I couldn't find any documentation where this is formally specified.
Can an experimented PHP dev confirm or disconfirm what I found in my trials?

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

What is this operator "=>"? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does “=>” mean in PHP?
Reference - What does this symbol mean in PHP?
I see this symbol in a lot of PHP code.
I can't figure out what it means or what it does..
Is it really an operator ?
Do you mean =>? If so, it's for initializing array keys (or indexes if you prefer). Like this:
$values = array(
'foo' => 'bar'
);
This will initiazlie an array with a key named foo with a value of bar.
Read more on about arrays at php.net.
http://php.net/manual/en/language.operators.php

Categories