PHP-在IF语句中使用文本文件中的一行

PHP-在IF语句中使用文本文件中的一行,php,Php,我正在尝试获取一个PHP文件,以便从文本文件中读取特定的行,然后在if语句的字符串比较中使用该行。 文本字段中的第二行将始终具有两个不同值中的一个。&activeGame=0或&activeGame=1 文本文件: boardarray=["NV", "SB", "VB", "NV"] &activeGame=1 &activePlayer=V PHP文件: $theFile = "test.txt"; $line = file($theFile); echo $l

我正在尝试获取一个PHP文件,以便从文本文件中读取特定的行,然后在if语句的字符串比较中使用该行。
文本字段中的第二行将始终具有两个不同值中的一个。
&activeGame=0
&activeGame=1

文本文件:

boardarray=["NV", "SB", "VB", "NV"]  
&activeGame=1  
&activePlayer=V  
PHP文件:

$theFile = "test.txt";
$line = file($theFile);
echo $line[1]; //This will output "&activeGame=1" without quotation marks

if ($line[1] == "&activeGame=1") {
    echo "The game is active";
} else {
    echo "The game is not active";
}
由于
echo$line[1]
输出
&activeGame=1
我知道PHP脚本可以从文本文件读取数据。
问题是if函数会回显“游戏未激活”,我不知道为什么

编辑
解决方案:

$theFile = "test.txt";
$line = file($theFile);
echo $line[1]; //This will output "&activeGame=1" without quotation marks

if (trim($line[1]) == "&activeGame=1") { 
    echo "The game is active";
} else {
    echo "The game is not active";
}

第5行的修剪功能缺失。

您的问题是文件的每一行都以
\n
结尾

如果您
var\u dump($line[1])
而不是echo它,您可以看到它

所以
&activeGame=1
的实际值是
&activeGame=1\n
。 这绝对不等于
&activeGame=1

因此,在比较之前-使用
trim
功能:

$theFile = "test.txt";
$line = file($theFile);
echo $line[1]; //This will output "&activeGame=1" without quotation marks

$line_one = trim($line[1]);
if ($line_one == "&activeGame=1") {
    echo "The game is active";
} else {
    echo "The game is not active";
}

您的问题是文件的每一行都以
\n
结尾

如果您
var\u dump($line[1])
而不是echo它,您可以看到它

所以
&activeGame=1
的实际值是
&activeGame=1\n
。 这绝对不等于
&activeGame=1

因此,在比较之前-使用
trim
功能:

$theFile = "test.txt";
$line = file($theFile);
echo $line[1]; //This will output "&activeGame=1" without quotation marks

$line_one = trim($line[1]);
if ($line_one == "&activeGame=1") {
    echo "The game is active";
} else {
    echo "The game is not active";
}

我会以这种方式使用
parse_str
,即使该行上有更多变量,您也可以始终获得该值


我会以这种方式使用
parse_str
,即使该行上有更多变量,您也可以始终获得该值


第一个问题是,如果您回显$line[1],则其值为“&activeGame=1”(请注意结尾处的空格。将为您提供所需输出的最佳解决方案代码如下所示

<?php
$theFile = "test.txt";
$line = file($theFile);
echo trim($line[1]); //This will output "&activeGame=1" without     quotation marks

$a=trim($line[1]);

$truestr="&activeGame=1";


if ($a == $truestr) {
    echo "The game is active";
} else {
    echo "The game is not active";
}
?>

输出
“&activeGame=1”游戏处于活动状态

第一个问题是,如果您回显$line[1],则其值为“&activeGame=1”(请注意结尾处的空格。将为您提供所需输出的最佳解决方案代码如下所示

<?php
$theFile = "test.txt";
$line = file($theFile);
echo trim($line[1]); //This will output "&activeGame=1" without     quotation marks

$a=trim($line[1]);

$truestr="&activeGame=1";


if ($a == $truestr) {
    echo "The game is active";
} else {
    echo "The game is not active";
}
?>

输出
“&activeGame=1”游戏处于活动状态

可能您在.txt文件中的值上有空格下面的答案很好…附带建议是使用单引号(未计算)与双引号(计算)。
如果($line[1]=”&activeGame=1'){
。在这种情况下,它不会影响结果。请确保使用
var_dump()
对两者进行比较时,它们是相同的。可能来自.txt文件的值上有空格。下面的回答很好…附带建议是使用单引号(未计算)和双引号(已计算)。
if($line[1]=='&activeGame=1'){
。在这种情况下,它不会影响结果。请确保使用
var\u dump()比较它们时,它们是相同的
在这两种情况下,它将始终以writenYes的形式返回true是的,我只是告诉您有关空间和正确工作的true条件,让我再次更新代码以解决逻辑问题,是的。尽管这
$FalsStr=“&activeGame=0”
是多余的,因为我会认为任何不匹配true的内容都是false(根据您的else语句)那么为什么要分配一个
$falsestr
@Dave是的,你是对的。它在以后的代码中可能有用,但在这里看起来是多余的。更新了code@Heemanshu巴拉-我注意到在第[1]行的末尾有一个空格,但当我试图将它与“&activeGame=1”(带空格)进行比较时,我仍然得到“游戏未激活”。但是修剪部分解决了问题!我已经更新了原始帖子,以便未来的谷歌用户也能获得帮助。谢谢!这将始终返回true作为writen是的,我只是告诉你空间和正确工作的真实条件,让我再次更新代码,解决逻辑问题,是的。尽管这
$false str=“&activeGame=0”;
是多余的,因为我认为任何不匹配true的东西都是false(根据您的else语句),所以为什么要分配一个
$false str
@Dave是的,您是对的。它在进一步的代码中可能有用,但在这里看起来是多余的。更新了code@Heemanshu巴拉-我注意到在这行的末尾有一个空格[1],但当我尝试将其与“&activeGame=1”(带空格)进行比较时,我仍然得到了“游戏未激活”。但修剪部分解决了问题!我已更新了原始帖子,以便未来的谷歌用户也能获得帮助。谢谢!