如何将变量设置为字符串?

时间:2014-03-05 15:59:44

标签: php string

有可能使用PHP制作类似的东西吗?

<?php

$number = 1;

$str_$number = "BlahBlah";

// and make this: $str_1 = "BlahBlah";

echo $str_1;

?>

4 个答案:

答案 0 :(得分:2)

<?php
$number = 1;
${'str_'.$number} = 'foobar';
echo $str_1;//foobar

答案 1 :(得分:1)

尝试

<?php
$number = 1;
${'str_' . $number} = 'foobar';
?>

答案 2 :(得分:1)

<?php
$number = 1;
$value = 'str_' . $number;
$$value = 'blahblah';
echo $str_1;
?>

答案 3 :(得分:0)

您也可以尝试

$number = 1;
eval('$str_'.$number.' = "foobar";');
echo $str_1;