2

How can I get value from variable which is string ?

$Member_Student        = 3600;
$selectedItem = "Member_Student";
$price = "$" . $selectedItem;
print_r($price); //prints $Member_Student instead of 3600

I cannot use eval function.

4 Answers 4

10

Use curly braces to denote a variable:

$Member_Student        = 3600;
$selectedItem = "Member_Student";
$price = ${$selectedItem};
print_r($price); // prints 3600
Sign up to request clarification or add additional context in comments.

Comments

8

use 2 $ signs:

var_dump($$selectedItem)

Comments

1

To get a variable from another variable containing its name use print_r($$selectedItem );

Comments

1

Change

$selectedItem = "Member_Student";

to

$selectedItem = "$Member_Student";

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.