Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does $$ in php mean? [duplicate]

Tags:

php

what does two back to back $ behind a variable means. Like this $$id

where can I find more information on that Thanks

like image 750
Asim Zaidi Avatar asked Jun 23 '26 10:06

Asim Zaidi


2 Answers

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.

like image 165
Carson Myers Avatar answered Jun 26 '26 10:06

Carson Myers


These are called variable variables.

$foo = 'bar';
$id = 'foo';

echo $id;  // prints foo
echo $$id; // prints bar
like image 44
codaddict Avatar answered Jun 26 '26 08:06

codaddict



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!