PHP variable value from string

2020-02-13 04:21发布

问题:

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.

回答1:

Use curly braces to denote a variable:

$Member_Student        = 3600;
$selectedItem = "Member_Student";
$price = ${$selectedItem};
print_r($price); // prints 3600


回答2:

use 2 $ signs:

var_dump($$selectedItem)


回答3:

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



回答4:

Change

$selectedItem = "Member_Student";

to

$selectedItem = "$Member_Student";