2

I have this situation:

$string = "temp";
$$string = array();

var_dump($temp);

$temp[0] = "hello";
var_dump($temp);

That works. But when accessing the array with a variable variable-name, I always get an empty array:

$string = "temp";
$$string = array();

$$string[0] = "prova";
var_dump($$string);

How can I access the array with a variable name?

4
  • This is what PHP does: ${$string[0]} = "prova"; See: php.net/manual/en/migration70.incompatible.php Commented Mar 14, 2016 at 12:56
  • this code : ${$string[0]} = "prova"; var_dump(${$string}); return empty array Commented Mar 14, 2016 at 12:58
  • but I think it will return empty again Commented Mar 14, 2016 at 12:58
  • 1
    @bomberdini Exactly, that is what PHP does if you don't define it otherwise. What you want and is also by default in PHP 7 is: ${$string}[0] = "prova"; Commented Mar 14, 2016 at 12:59

3 Answers 3

2

Because when you used $$ then it means that you are referring variables.

  $string = "temp";
  var_dump($$string);

Here $$string means that $temp and that is not having value so it is empty result.

In case if you wants to work then use as you suggested.

Use the code as follows:

${$string[0]} = "prova";
var_dump(${$string[0]});

output: string(5) "prova"

OR

${$string}[0] = "prova";
var_dump($$string[0]); 

output: array(1) { [0]=> string(5) "prova" }

This way you can use $$ variables of variable in php

Reference Link

Sign up to request clarification or add additional context in comments.

2 Comments

ok this code work : ${$string[0]} = "prova"; var_dump(${$string[0]});
Shouldn't it be ${$string}[0]?
1
$string = "temp";
$$string = array();

$$string[0] = "prova";//here first it get the value of $string[0] and then value of variable of string $string[0], but $string[0] is nothing here that's why you are getting empty in below line
var_dump($$string);
// To get as you want use below line
${$string}[0] = "prova";
var_dump($$string);

Check here : https://eval.in/536239

Comments

1

When you use $$string[0], you are calling a variable named in an array called $string. Since strings are basically complicated array-like objects, you are calling the character at position 0. So basically, the effect you get is:

$string = "temp";
$$string = array();

$$string[0] = "prova";
var_dump($t);

because the reference for $string[0] is the first character in the string "temp", or "t", so you are really just calling $t. What you need is ${$string}[0].

If you want the variable to be an array:

$var_name = "some_var";
${$var_name} = array();

${$var_name}[0] = "some value";
${$var_name}[1] = "another value";
var_dump(${$var_name});

If you want your name of your variable to be an array:

$var_names = array();
$var_names[0] = "some_var";
$var_names[1] = "another_var";

${$var_names[0]} = "some value";
${$var_names[1]} = "another value";

var_dump(${$var_names[0]});
var_dump(${$var_names[1]});

The first is an example of what I think you were trying to do. The second is an example of what you were actually doing.

Or, you could just use an associative array:

$my_vars = [
    "some_var" => "some value",
    "another_var" => "another value",
];
var_dump($my_vars);
//or loop
foreach ($my_vars as $key => $value)
{
    var_dump($my_vars[$key]);
    //or echo
    echo $key . ": " . $value . "<br>";
    //or set
    $my_vars[$key] = "new calculated value + " . $value;
}

var_dump($my_vars);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.