即使文本字段为空,isset()也会计算为true.这是为什么?

Suh*_*pta 3 php isset

第一个片段从两个文本字段中获取数据并发送到action script.php.if即使我没有在文本字段中输入任何内容,问题是语句评估为true.这是为什么 ?

try.php

<form method='get' action='./action_script.php'>
        <input type="text" id="text_first" name="text_first" /> <br />
        <input type="text" id="text_second" name="text_second"/> <br />
        <input type="submit" id="submit" />
</form>
Run Code Online (Sandbox Code Playgroud)
action_script.php

<?php

  if(isset($_GET['text_first'])) {
        echo "Data from the first text field : {$_GET['text_first']} <br>";
  }
  if(isset($_GET['text_second'])) {
        echo "Data from the second text field : {$_GET['text_second']} <br>";
  }

  echo "After the if statement <br />";
Run Code Online (Sandbox Code Playgroud)

Zol*_*oth 8

因为它们都已设置 - 变量存在于$_GET数组中.即使它们的值是空字符串.

尝试检查是否有异常

 if( isset($_GET['text_first']) && $_GET['text_first'] !== '' ) 
Run Code Online (Sandbox Code Playgroud)

要么

if ( ! empty( $_GET['text_first'] ) ) {
Run Code Online (Sandbox Code Playgroud)

请注意,您不需要使用,isset()因为empty()如果变量不存在则不会生成警告.