如何更改循环中的php变量名? [英] how to change php variable name in a loop?
问题描述
假设我有一个名为$ file的变量,for循环为:($ i = 1; $ i <= 5; $ i ++){}
对于for循环的每次迭代, $ i
值将被附加到 $ file
变量名,所以在for循环结束后,我应该有五个变量: $ file1
, $ file2
,
$ file3
, $ file4
和 $ file5
。
使用 $ {'varname'}语法: 然而,使用数组通常更好。 Lets say I have a variable called $file and the for loop: For each iteration of the for loop, the Use ${'varname'} syntax: However, it's often better to use arrays instead of this. 这篇关于如何更改循环中的php变量名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ p $ for($ i = 1; $ i <= 5; $ i ++){
$ {'file'。 $ i} = $ i;
$ / code> for($i=1; $i <= 5; $i++) {}
$i
value will be appended to the $file
variable name so after the for loop ends, I should have five variables: $file1
, $file2
, $file3
, $file4
, and $file5
.for($i=1; $i <= 5; $i++) {
${'file' . $i} = $i;
}