检查 $_POST-value 是否为空 [英] Check whether $_POST-value is empty

查看:25
本文介绍了检查 $_POST-value 是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if(isset($_POST['submit'])) {
    if(!isset($_POST['userName'])) {
        $username = 'Anonymous';
    }      
    else $username = $_POST['userName'];
}

我无法将 $username 设为匿名"?它要么是空白,要么是 $_POST['userName'] 的值.

I cannot get the $username to be "Anonymous"? It is either blank or the value of $_POST['userName'].

推荐答案

isset() 如果变量已经初始化,将返回真.如果您的表单字段的 name 值设置为 userName,则提交该表单时,该值将始终为设置",尽管其中可能没有任何数据

isset() will return true if the variable has been initialised. If you have a form field with its name value set to userName, when that form is submitted the value will always be "set", although there may not be any data in it.

相反,trim() 字符串并测试其长度

Instead, trim() the string and test its length

if("" == trim($_POST['userName'])){
    $username = 'Anonymous';
}      

这篇关于检查 $_POST-value 是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆