0

I need to make a dynamic variable based on a loop, what i am looking for is a variable something like

$var = "foo";

$$var = "bar";

echo $foo; // bar

but for me it should be more like a fixed parameter attached to the dynamic part like

$var='123';

$'current_'.$$var=some value; // not correct syntax

echo $current_123 should give 'some value';

1 Answer 1

3

Use curly braces:

$${'current_' . $var} = $some_value;
Sign up to request clarification or add additional context in comments.

1 Comment

That is the recommended way.. but variable variables are nasty if you're not careful. They're harder to read, trace, and debug. If you must use them, please document it for the next guy.

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.