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

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"

Related

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).

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

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.

What does $$ in php mean? [duplicate]

This question already has answers here:
What does $$ (dollar dollar or double dollar) mean in PHP?
(7 answers)
Closed 8 years ago.
what does two back to back $ behind a variable means. Like this
$$id
where can I find more information on that
Thanks
In PHP, $$ means you are about to inflict years of pain and suffering on at least one maintenance programmer. Note that you might wind up being that maintenance programmer.
It is a variable variable. Imagine this:
$quux = 'bar';
$foo[$quux] = "baz";
echo $foo['bar']; //prints baz
if there was no such thing as arrays, you might try something like this:
$quux = 'bar';
$$quux = "baz";
echo $bar; //prints baz
luckily we do have arrays so please don't use variable variables unless you are doing something convoluted and magical* and have no other choice.
*: Please don't do convoluted magical things, either.
These are called variable variables.
$foo = 'bar';
$id = 'foo';
echo $id; // prints foo
echo $$id; // prints bar
in the PHP manual of course
http://www.php.net/manual/en/language.variables.variable.php
note that it's obsolete and senseless syntax and you should always use arrays instead.

Is it possible to do dynamic variables in ruby? [duplicate]

This question already has answers here:
How to dynamically create a local variable?
(4 answers)
Closed 3 years ago.
I can accomplish this dynamic nature in other ways, but it caused me to be curious. Is there a similar mechanism to this in Ruby?
$varname = "hello";
$$varname = "world";
echo $hello; //Output: world
You can achieve something similar using eval
x = "myvar"
myvar = "hi"
eval(x) -> "hi"
It's possible only for instance variables (and class variables):
class MyClass
def initialize
#varname = :"#hello"
instance_variable_set #varname, "world"
end
def greet
puts instance_variable_get(#varname)
end
end
MyClass.new.greet
#=> "world"
For local variables you have to use eval.

Categories