1

Suppose i have the following variable

$a="stack";

$b="overflow";

now i want to create another variable using the variable $a and $b .I want the new variable to be $stack_overflow ="The best place to learn programming";

I tried the following to create the variable $stack_overflow but it didn't work

${$a."_".$b}="The best place to learn programming";
echo $stack_overflow;
2
  • It worked fine for me: codepad.viper-7.com/XBlOHB Commented Dec 17, 2014 at 18:04
  • Your updated code works! Commented Dec 17, 2014 at 18:06

2 Answers 2

2

This should work for you:

<?php

    $a = "stack";
    $b = "overflow";
    $name = $a . "_" . $b;

    $$name = "The best place to learn programming";
    echo $stack_overflow;

?>

Output:

The best place to learn programming

For more information see: http://php.net/manual/en/language.variables.variable.php

EDIT:

Your updated code:

${$a."_".$b}="The best place to learn programming";
echo $stack_overflow;

works too.

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

Comments

0

Actually my code works and even @Rizier123 code does as well(in this simplifed example)

I did a mistake in naming my variables so it would have never worked in my application

1 Comment

I would recommend you to edit your question(stackoverflow.com/posts/27531913/edit) and write this!(mark it as an edit if you do) Otherwise some people may down vote this.(from my experience)

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.