检查变量是否为空或空字符串的更好方法? [英] Better way to check variable for null or empty string?
本文介绍了检查变量是否为空或空字符串的更好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
既然PHP是一种动态语言,那么检查提供的字段是否为空的最佳方法是什么?
Since PHP is a dynamic language what's the best way of checking to see if a provided field is empty?
我要确保:
- null被认为是空字符串
- 只有空格的字符串被认为是空的
- "0"不被认为是空的
这是我到目前为止所得到的:
This is what I've got so far:
$question = trim($_POST['question']);
if ("" === "$question") {
// Handle error here
}
必须有一种更简单的方法吗?
There must be a simpler way of doing this?
推荐答案
// Function for basic field validation (present and neither empty nor only white space
function IsNullOrEmptyString($str){
return (!isset($str) || trim($str) === '');
}
这篇关于检查变量是否为空或空字符串的更好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文