4

I am currently debugging a PHP OpenCart plugin and I have came across syntax I have never came across before.

I am interested in what this does and why it's used, and links to any documentation. The culprit is below:

${$variable.'s_array'}
2
  • 4
    It's a "variable" variable and actually depends on the value of $variable. So let's say $variable is "hello", it becomes ${'hellos_array'} then $hellos_array. Commented Nov 12, 2015 at 14:56
  • PHP doc: php.net/manual/en/language.variables.variable.php Commented Nov 12, 2015 at 15:00

1 Answer 1

7

Variables in PHP can have variables in their declaration, like this:

e.g. try running this code-snippet:

$var = "dog_name";
$$var = "golden terrier";
echo $dog_name; //gives "golden terrier"

now to your case:

$variable = "random_";
${$variable.'s_array'} = "somecontent";
echo $random_s_array; //gives "somecontent"

this will give you dynamic variables.

Try this Sandbox-Example :)

PHP-Doc: http://php.net/manual/en/language.variables.variable.php (mentioned by versalle88)

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

1 Comment

Fantastic answer! Thank you so much :-)

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.