如何将 $ 符号添加到字符串以使其成为变量?
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
如何将 $ 符号添加到字符串以使其成为变量?
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
不是达到该值的最佳方式,但 PHP 支持:$$var :)
为什么不只是:
if ($_GET['val'] == 'customer') {
function($bar);
}
$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'] 的值是否是您(程序员)期望的值,而不是其他值。
只需执行以下操作:
$var = '我的字符串';
${$var} = '输出';
回声 $myString;
好的,这是您绝对应该包括的白名单。
$whitelist = array('customer', 'consumer');
$fallback = $whitelist[0];
$var = in_array($whitelist, $_GET['val'] ? $_GET['val'] : $fallback;