如何将$符号添加到字符串中以使其成为变量?
Eg:
$consumer = array()
$industrial = array()//These 2 are in a separate include file.
$var = $_GET['val'] // value here is 'consumer'
function ('$'.$var,$bar) //I'm trying to make consumer -> $consumer
答案 0 :(得分:4)
不是达到该值的最佳方式,但PHP支持:$$ var:)
答案 1 :(得分:4)
答案 2 :(得分:0)
为什么不呢:
if ($_GET['val'] == 'customer') {
function($bar);
}
答案 3 :(得分:0)
$consumer = array()
$industrial = array()//These 2 are in a separate include file.
$var = $_GET['val'] // value here is 'consumer'
function ($$var,$bar) //I'm trying to make consumer -> $consumer
不要忘记检查$ _GET ['val']的值是否是您(程序员)期望的值,而不是其他任何值。
答案 4 :(得分:0)
请执行以下操作:
$ var ='myString';
$ {$ var} ='output';
echo $ myString;
答案 5 :(得分:0)
好的,这是白名单你应该明确包括。
$whitelist = array('customer', 'consumer');
$fallback = $whitelist[0];
$var = in_array($whitelist, $_GET['val'] ? $_GET['val'] : $fallback;