如何在 PHP 中的循环中调用编号变量


how to call a numbered variable in a loop in php?

我知道我在这里实现的内容是错误的,我希望它正确地完成它,这就是为什么在这里寻求帮助。不知道这是否可能。

<?php
$test1="hello";
$test2="how";
$test3="are";
for($i=1;$i<=3;$i++)
{
    echo $test.$i;
}
?>

当我运行它时,我应该得到hello how are.我知道字符串连接同样的事情,我也想为变量做这件事。这是可能的吗,如果可能的话,我可以轻松访问所有这些变量。有什么帮助吗?

尝试使用以下语法:

echo ${'test'.$i};

我想你要找的是"数组"。你可以在 PHP 中使用数组,如下所示:

$test = array('hello', 'how', 'are');
$len = count($test);
for($i=0; $i<$len; $i++) {
   echo $test[$i];
}

试试这个:

for($i = 1; $i < 4; $i++){
    echo $test{$i};
}