how to change php variable name in a loop?

前端 未结 5 1292
天命终不由人
天命终不由人 2020-12-05 04:58

Lets say I have a variable called $file and the for loop: for($i=1; $i <= 5; $i++) {}

For each iteration of the for loop, the $i value w

5条回答
  •  一向
    一向 (楼主)
    2020-12-05 05:28

    Use ${'varname'} syntax:

    for($i=1; $i <= 5; $i++) {
        ${'file' . $i} = $i;
    }
    

    However, it's often better to use arrays instead of this.

提交回复
热议问题