Php 如何增加变量,使其在While循环中从文本文件中选择不同的行

Php 如何增加变量,使其在While循环中从文本文件中选择不同的行,php,while-loop,Php,While Loop,我正在写剧本。下面是发生的事情。有一个while循环。while循环中有一个变量,它是X的常量。如何在while循环的每个循环中使X从第1行、第2行等更改,并从.txt文件中提取X。万物皆有根。谢谢这是伪代码柯克船长: //we assume current working directory is root fileHandle = openFile("Read","some.txt"); X = pull("X",fileHandle); while( X is constant ) {

我正在写剧本。下面是发生的事情。有一个while循环。while循环中有一个变量,它是X的常量。如何在while循环的每个循环中使X从第1行、第2行等更改,并从.txt文件中提取X。万物皆有根。谢谢

这是伪代码柯克船长:

//we assume current working directory is root
fileHandle = openFile("Read","some.txt");
X = pull("X",fileHandle);

while( X is constant )
{
  XFactor = factor(X);
}

我可以通过更多关于您来自哪个宇宙的细节、您打算使用的编程语言以及关于您希望发生什么的细节来完善和改进这一点

这是伪代码柯克船长:

//we assume current working directory is root
fileHandle = openFile("Read","some.txt");
X = pull("X",fileHandle);

while( X is constant )
{
  XFactor = factor(X);
}
$f = fopen("some.txt", "r");
while (!feof($f) && $some_condition) {
    $x = fgets($f);
    // do something
}
fclose($f);
我可以通过更多关于您来自哪个宇宙的细节、您打算使用的编程语言以及关于您希望发生什么的细节来完善和改进这一点

$f = fopen("some.txt", "r");
while (!feof($f) && $some_condition) {
    $x = fgets($f);
    // do something
}
fclose($f);
这是否足够

//get the lines of the file into an array
$file_array = file($file_name);

//go through the array line by line
foreach ($file_array as $line_number => $line)
{
  //you didn't tell us what you are doing with each line
  //so you will need to change this to your liking
  $X = $line; // Handle the line
}
这是否足够

//get the lines of the file into an array
$file_array = file($file_name);

//go through the array line by line
foreach ($file_array as $line_number => $line)
{
  //you didn't tell us what you are doing with each line
  //so you will need to change this to your liking
  $X = $line; // Handle the line
}
编辑:注意:对于非常大的文件,这可能不是一个好方法,因为这将一次性将整个文件加载到内存中


编辑:注意:对于非常大的文件,这可能不是一个好方法,因为这将一次性将整个文件加载到内存中。

坦白说,我不知道您想问什么。你用什么语言?您只是想迭代文本文件中的所有行,还是“从.txt文件中提取X”是什么意思?什么是“根”?您可以发布您的脚本(或者,最好是显示问题的脚本的简化版本)?Ok while(此条件为真){$x=txt文件下一行;};它是php。坦白说,我不知道你想问什么。你用什么语言?您只是想迭代文本文件中的所有行,还是“从.txt文件中提取X”是什么意思?什么是“根”?您可以发布您的脚本(或者,最好是显示问题的脚本的简化版本)?Ok while(此条件为真){$x=txt文件下一行;};它是php。我怎么写呢?我觉得投票很糟糕,但我笑得很大声。我觉得投票很糟糕,但我笑得很大声。