Meaning of extra $ variable in PHP [duplicate] - php

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

Related

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 ${$?} Could someone shed some light? [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 6 years ago.
I'm touching up on some PHP that I sometimes forget (keep the old brain going) and came across this in a PHP questionnaire. It goes as follows:
<?php
$a = "b";
$b = "a";
print ${$b} ;
//$b = "b"
?>
How does this work and how would I use it practically? Thank you in advance.
This is a variable variable
print ${$b}
It first evaluates {$b} and gets 'a'. So then it evaluates $a and gets "b" (the value stores in $a).

what is a variable with two $ sign ? $$ myVar [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 9 years ago.
I've seen $$var in a php test for a job interview. I was wondering what was it ..
Thanks
Variable variables - check the link out!
Variable variables
$var = 'something';
$$var = 'lol';
echo $something will output "lol"
it's a variable variable.
This will refer to the value of particular variable.
Eg :
$test = "Hi";
$var = "test";
Now $$var will display "Hi"

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.

i got this PHP question from my friend about variable dollar sign [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
what does $$ mean in PHP?
what is the different between $thisvariable and $$thisvariable. as you notice, the first variable has one dollar sign while the second got two dollar signs.
$variable is a variable and $$variable is a variable variables,
$my_name = "anthony"; // variable $my_name
echo $my_name; // outputs "anthony"
$a_var = "my_name"; // assigning literal to variable $a_var
echo $$a_var; // outputs "anthony"
It may be a bit confusing, so let's break down that echo call,
$($a_var)
-> $(my_name)
-> $my_name = "anthony"
Please note that the above may not be what happens behind the scenes of the PHP interpreter, but it serves strictly as an illustration.
Hope this helps.
$thisvariable is a variable named $thisvariable:
$thisvariable = 'Hello';
print $thisvariable; // prints Hello
$$thisvariable is a variable variable:
$thisvariable = 'Hello';
$Hello = 'Greetings';
print $$thisvariable; // prints Greetings ($$thisvariable is equivalent to $Hello)
For the most part you should avoid using variable variables. It makes the code harder to understand and debug. When I see it it's a red flag that there's some bad design.

Categories