Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP if语句从txt读取并启用选中按钮_Php_Html_Button - Fatal编程技术网

PHP if语句从txt读取并启用选中按钮

PHP if语句从txt读取并启用选中按钮,php,html,button,Php,Html,Button,我有一个PHP的开关按钮和一个txt文件。当txt文件中的值为true时,我希望按钮被禁用(未选中echo),当txt文件中的值为false时,我希望按钮被启用(选中echo) 我尝试了下面的代码,但它始终保持未选中状态。我做错了什么 <?php $wifi2g=file_get_contents("wifi2g.txt"); ?> <link rel="stylesheet" type="text/css" href="style.css"> <div clas

我有一个PHP的开关按钮和一个txt文件。当txt文件中的值为true时,我希望按钮被禁用(未选中echo),当txt文件中的值为false时,我希望按钮被启用(选中echo) 我尝试了下面的代码,但它始终保持未选中状态。我做错了什么

<?php
$wifi2g=file_get_contents("wifi2g.txt");
?>

<link rel="stylesheet" type="text/css" href="style.css">
<div class="onoffswitch">
    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" <?php if ($wifi2g = 'true') {echo "unchecked"; } else  { echo "checked";}  ?> >
    <label class="onoffswitch-label" for="myonoffswitch">
        <span class="onoffswitch-inner"></span>
        <span class="onoffswitch-switch"></span>
    </label>
</div>


一个常见的错误可能会在某个时候让我们所有人都陷入困境。您需要使用2个等于,因为
如果($var='true'){
将始终为true,例如,将var赋值为true,如果成功(始终),则

此外,您不需要“取消选中”,因为如果“选中”不在标记中,它将不会被选中,因此您不需要else

<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox"
   id="myonoffswitch" <?php if ($wifi2g == 'true') {echo "checked";} ?> >

如果该文件中只有一行文本(true),根据
var_dump()
的结果,该行文本似乎包含空格,则可以使用
trim()
注释“i get string(6)“true”–La Erti B


如果文件中包含空格,则可以使用该选项或删除文件中的空格

然而,另一种可能的情况是,它下面可能有一个换行符,这将解释额外的2个字符

如果这两种情况都是这样,
trim()
仍然可以工作

另外,如前所述,
if($wifi2g='true')
的使用是一种赋值,而不是比较

if($wifi2g=='true')


写入文件是另一种可能的情况

我们首先不知道该文件是如何写入的。其中一个可能的原因是,在此期间可能使用了
“\n”

“\n”
是换行字符

如果是这种情况,则需要将其删除,或者如我所述使用
trim()

  • 如果文件包含2个尾随空格,或者没有空格但在文本行下有换行符,则两者都将生成
    string(6)“true”

  • 这些都是决定性的结果。


=
用于赋值,
=
用于比较。如果($wifi2g=='true')
,则应该是
如果($wifi2g=='true')
。另外:为什么要为此使用文本文件,而不仅仅是另一个php文件,如果($wifi2g=='true'){echo“未选中”}则可以包含该文件。{echo“已选中”}尝试了这种方式,它现在总是处于选中状态。它是.txt文件中的一个字符串。在回答之前,您应该阅读注释线程
<?php
$wifi2g=file_get_contents("wifi2g.txt");
$wifi2g = trim($wifi2g);
?>