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.
Related
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
This question already has answers here:
Stop using `global` in PHP
(6 answers)
Closed 7 years ago.
The $GLOBALS array of PHP provides access to all global variables, like
<?php
$foo = 'hello';
function myFunc() {
echo $GLOBALS['foo']; // prints "hello"
}
?>
Now I have to work on some code from other people that directly adds elements to the array to have it available globally (without having an according variable), like so:
<?php
function doSomething() {
// $newData is NOT existing anywhere!
$GLOBALS['newData'] = 'Hello World!';
// now $GLOBALS['newData'] is available anywhere else w/o actual variable
}
?>
Until now I never saw this specific usage of the $GLOBALS array and was wondering if it is considered "safe" or "good"? The PHP manual makes no statement about writing to this array, only about reading from it.
Opinion perhaps, but I would think it's better instead to use define instead of the $GLOBALS array if you're looking to define a constant for use in other parts of your application.
PHP:define - Manual
<?php
function doSomething() {
$_POST['__newData'] = 'Hello World!';
}
doSomething();
echo $_POST['__newData'];
?>
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"
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.
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.