开发者

PHP concatenate variable

Might be an easy question for you guys. can't find it on google.

I am trying to c开发者_如何学Pythononcatenate two variables name;

$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.

I know I can't use $test.$i but don't know how to do this.Any helps? Thanks!


try ${$test.$i} = value

EDIT: http://php.net/manual/en/language.variables.variable.php


I'm assuming that the variables are called $test0, $test1, ..., $test5. You can use the following:

${"test".$i}

Though, might I suggest that you make $test an array and use $i as the index instead? It's very odd to use $i as an index to loop through a list of variable names.

As an example, instead of:

$test0 = "hello";
$test1 = "world";

Use:

$test[0] = "hello";
$test[1] = "world";


Try this:

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


This might work:

$varName = $test . $i;
$$varName = ...

Can i ask where for this is neccesary?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜