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

查看:111
本文介绍了检查$ _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() <如果变量已初始化,则/ a>将返回true。如果您的表单字段的名称值设置为 userName ,则在提交该表单时,该值将始终为set,虽然可能没有任何数据。

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天全站免登陆