以下是变量:
$fake= 'cool';
$fake1 = 'not cool';
$hope= '1';
这个想法是结合$fake
并$hope
创建变量$fake1
。这个想法是,如果$hope
变量是随机的,它可以生成一个随机变量:$fake1
,$fake2
,$fake3
,等等。现在我要么得到一个错误,要么只是彼此相邻的值$fake
而不是新变量。$hope
您应该为此使用“数组”:
$list = array('cool', 'not cool');
$random_item = array_rand($list);
使用以变量命名的变量总是很麻烦,而这正是数组的用途。
You can try
$fake = array(
"fake1" => "Cool",
"fake2" => "Bad",
"fake3" => "Fish",
"fake4" => "Next",
"fake5" => "Wow");
list($a, $b) = array_rand($fake, 2);
echo $fake[$a] . " " . $fake[$b]; // This would always change
上面 Ben 的评论可能正是您想要的,但如果您使用的是 PHP5,您还可以执行以下操作:
$varname = $fake . $hope;
$$varname = "horray";