如何在PHP中按条件获取输入?

如何在PHP中按条件获取输入?,php,Php,通常在PHP中,为了在PHP中获取输入,我们使用finction: readline() 获得所有可用的输入 例如,如果我想在用户输入“end”之前获取输入,我们怎么做?您需要做的是获取读取行,找到“end”标记,然后修剪字符串 $endTag = "end"; //Set the end tag $line = readline(); //Read the input $endPos = strpos($line, $endTag); //Find the locat

通常在PHP中,为了在PHP中获取输入,我们使用finction:

  • readline()
获得所有可用的输入


例如,如果我想在用户输入“end”之前获取输入,我们怎么做?

您需要做的是获取读取行,找到“end”标记,然后修剪字符串

$endTag = "end"; //Set the end tag
$line = readline(); //Read the input

$endPos = strpos($line, $endTag); //Find the location of your endTag
echo substr($line, 0, $endPos); //Get the substring of your line from position 0 up to the location of your endTag

或者,您也可以执行和分解($endTag,$line),将用户的输入拆分为一个数组,只使用第一项,但一般认为这有点“干净”

只需执行“无限循环”,直到达到所需的关键字

$keyword='end';
$line=null;
while($line!==$keyword){
$line=readline();
/* ... */
}

而($line!=='end')readline()
为什么不直接将其与
end
进行比较?你不想让输入
无休止的输入
结束输入序列你是对的,我本来打算在答案上加上这个,但问题是出现了“结束”之类的词……谢谢你们的指导^_^