gpt4 book ai didi

php - 即使文本字段为空,isset() 的计算结果也为真。这是为什么?

转载 作者:可可西里 更新时间:2023-11-01 00:14:57 26 4
gpt4 key购买 nike

第一个片段从两个文本字段中获取数据并发送到 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>

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 />";

最佳答案

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

也尝试检查是否为空

 if( isset($_GET['text_first']) && $_GET['text_first'] !== '' ) 

if ( ! empty( $_GET['text_first'] ) ) {

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

关于php - 即使文本字段为空,isset() 的计算结果也为真。这是为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15500012/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com