Double Dollar Sign PHP

What does $$ (dollar dollar or double dollar) mean in PHP?

A syntax such as $$variable is called Variable Variable.

For example, if you consider this portion of code:

$real_variable = 'test';
$name = 'real_variable';
echo $$name;

You will get the following output:

test

Here:

  • $real_variable contains 'test'
  • $name contains the name of your variable: 'real_variable'
  • $$name mean "the variable thas has its name contained in $name"
    • Which is $real_variable
    • And has the value 'test'

EDIT after @Jhonny's comment:

Doing a $$$?

Well, the best way to know is to try ;-)

So, let's try this portion of code:

$real_variable = 'test';
$name = 'real_variable';
$name_of_name = 'name';

echo $name_of_name . '<br />';
echo $$name_of_name . '<br />';
echo $$$name_of_name . '<br />';

And here's the output I get:

name
real_variable
test

So, I would say that, yes, you can do $$$ ;-)

Double dollar sign php

The problem with $$ in PHP is that you create unknown variable names, that may override variable names you already use. It is a source for subtle programming errors, and should generally not be used.

PHP - Difference in creating variable variables with brackets or double dollar signs?

Both versions are basically the same.

The curly braces notation is used in cases where there could be ambiguity:

$foo = 'bar';
$bar = [];
${$foo}[] = 'foobar';

var_dump($bar); // foobar

If you'd omit the braces in the above example, you'd get a fatal error, with the braces, it works fine.

However, I would recommend avoiding dynamic variable names altogether. They are funny to use and may save some space. But in the end, your code will become less readable and you will have trouble debugging.

Double dollar variables with arrays

You were very close. You need to tell PHP to evaluate the $a alone. Try:

<?php

$a = 'test';
$test = ['a', 'b'];
echo ${$a}[0];

Example

How can I mimic PHP $$ (double dollar sign) in jQuery

If you can get the name of the relevant part of your object from the class of the a, then you can reference it like this: object["stringKey"] or object[variable]. Given your example:

// this object mimics the object passed to your plugin
var myobj = {
myobja: {
zero: 'obj00 zero',
one: 'obj01 one',
two: 'obj02 two'
},
myobjb: {
zero: 'obj10 aaa',
one: 'obj11 bbb'
}
}

var objectName = "myobja"; // This would be the value from the A elements' class
alert(myobj[objectName].zero)

Example fiddle

PHP dollar sign in strings

$str contains a string with the content of "$var" (no variable replacement, just these very characters). It was created using single quotes, so no variable replacement there.

When echoing it using echo "$str", the variable $str gets replaced with its content, namely the string "$var", thus resulting in your output.

The string replacement in double quotes strings does not work recursively! So in order to have $str replaced by 1024 in the second appearance, you have to create $str using double quotes in the first place.

Accessing object variable via double dollar sign

$this->data->$this->iterator is ambiguous. Do you mean the property of $this->data that has the name of $this->iterator or the iterator property of $this->data->$this?

You need to make it unambiguous:

$this->data->{$this->iterator}

The same goes for $$this->iterator.

JavaScript Double Dollar Sign

I wrote this function. :)

There is no longer any special significance to the $$ prefixes. It was a notation that I used back when packer was popular. Packer would shorten variable names with this prefix. Nowadays Packer will automatically shorten variable names so this notation is redundant.

Short answer, the $ sign is a valid identifier in JavaScript so you are just looking at a bunch of ugly variable names. ;)

Double dollar variable not being called correctly in a function

Warning Please note that variable variables cannot be used with PHP's Superglobal arrays within functions or class methods.

source



Related Topics



Leave a reply



Submit