检查“不为空"的PHP语法是什么?还是一个空字符串? [英] What is the PHP syntax to check "is not null" or an empty string?
问题描述
可能重复:
检查变量是否为空
Possible Duplicate:
Check if a variable is empty
一个简单的PHP问题:
Simple PHP question:
我有这句话:
if (isset($_POST["password"]) && ($_POST["password"]=="$password")) {
...//IF PASSWORD IS CORRECT STUFF WILL HAPPEN HERE
}
在此语句的上方,我在JavaScript中使用以下行将用户名设置为JavaScript和PHP中的变量:
Somewhere above this statement I use the following line in my JavaScript to set the username as a variable both in my JavaScript and in my PHP:
uservariable = <?php $user = $_POST['user']; print ("\"" . $user . "\"")?>;
我想做的是添加一个条件,以确保$ user不为null或为空字符串(它不必为任何特定值,我只是不希望它为空.正确的方法吗?
What I want to do is add a condition to make sure $user is not null or an empty string (it doesn't have to be any particular value, I just don't want it to be empty. What is the proper way to do this?
我知道这是一个棘手的问题,但是我没有使用PHP的经验.请指教,谢谢!
I know this is a sill question but I have no experience with PHP. Please advise, Thank you!
推荐答案
是空还是空字符串?
if (!empty($user)) {}
使用empty().
意识到$ user〜= $ _POST ['user'](感谢matt)之后:
After realizing that $user ~= $_POST['user'] (thanks matt):
var uservariable='<?php
echo ((array_key_exists('user',$_POST)) || (!empty($_POST['user']))) ? $_POST['user'] : 'Empty Username Input';
?>';
这篇关于检查“不为空"的PHP语法是什么?还是一个空字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!