如何使用php在for循环中创建变量列表

如何使用php在for循环中创建变量列表,php,for-loop,variables,Php,For Loop,Variables,在txt文件(translations.txt)中,我有一些绑定到变量的单词行。txt文件如下所示: All Articles Main Articles Previous Page Next Page // and so on... 要读取所有这些行的内容,我将它们放在一个数组中: $translationfile = 'data/translations.txt'; $lines_translationfile = file($translationfile, FILE_IGNORE_N

txt
文件(translations.txt)中,我有一些绑定到变量的单词行。txt文件如下所示:

All Articles
Main Articles
Previous Page
Next Page
// and so on...
要读取所有这些行的内容,我将它们放在一个数组中:

$translationfile = 'data/translations.txt'; 
$lines_translationfile = file($translationfile, FILE_IGNORE_NEW_LINES); // all lines of the txt file into an array
// bind content to variables
$translation0 = $lines_translationfile[0] // All Articles
$translation1 = $lines_translationfile[1] // Main Articles
$translation2 = $lines_translationfile[2] // Previous Page
$translation3 = $lines_translationfile[3] // Next Page
// and so on till 40
我尝试使用for循环生成这些变量:

for ($x = 0; $x <= 40; $x++) {
    $translation.$x = $lines_translationfile[$x]; // Does not work...
}

对于($x=0;$x我建议使用数组,但如果您想使用多个变量,请使用以下代码

for ($x=1; $x < 40; $x++) { 
    ${"translation".$x}=$lines_translationfile[$x];    
}
($x=1;$x<40;$x++)的

${“translation”。$x}=$lines\u translationfile[$x];
}

为什么需要这些变量?只需使用数组引用。这些变量的值可能会更改。我让用户进行翻译,他/她的翻译短语将存储在
translations.txt
文件中。然后我只需更改php文件的内容,例如“Main Articles”对于
$translation1
,这并不能解释为什么需要变量。在使用
$translation1
的地方,您可以同样轻松地使用
$lines\u translationfile[1]
,尽管我可能会为数组使用一个不太麻烦的名称。