PHP连接变量

Fly*_*Cat 6 php variables concatenation

对你们来说,这可能是一个简单的问题.在谷歌找不到它.

我试图连接两个变量名称;

$i=0;
 for ($i=0;$i<5;$i++){
   if($array[$i]>0){

   $test.$i=//do something
   }else{
  $test.$i=//do something
  }
}

//echo $test0 gives me nothing.
//echo $test1 gives me nothing.
Run Code Online (Sandbox Code Playgroud)

我知道我不能使用$ test.$ i但不知道怎么做.任何帮助?谢谢!

小智 8

我假设变量名为$ test0,$ test1,...,$ test5.您可以使用以下内容:

${"test".$i}
Run Code Online (Sandbox Code Playgroud)

虽然,我可以建议你让$ test一个数组并使用$ i作为索引吗?使用$ i作为循环遍历变量名列表的索引是很奇怪的.

作为一个例子,而不是:

$test0 = "hello";
$test1 = "world";
Run Code Online (Sandbox Code Playgroud)

使用:

$test[0] = "hello";
$test[1] = "world";
Run Code Online (Sandbox Code Playgroud)


nc3*_*c3b 6

试试这个:

 for ($i=0;$i<5;$i++){
    $the_test = $test.$i;
    if($array[$i]>0){
        $$the_test=//do something
    }
    else{
        $$the_test=//do something
    }
}
Run Code Online (Sandbox Code Playgroud)