php:如何检查一个字段是否有空白/空/NULL 值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2201452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:29:08  来源:igfitidea点击:

php : How to check a field have blank/empty/NULL value?

phpvalidation

提问by diEcho

I want to display an error when a variable have a BLANK value or EMPTY or NULL value. for example variable is shown below:

当变量具有 BLANK 值或 EMPTY 或 NULL 值时,我想显示错误。例如变量如下所示:

 $mo = strtotime($_POST['MondayOpen']);

and
var_dump($_POST['MondayOpen'])returns string(0) "".


var_dump($_POST['MondayOpen'])返回string(0) "".

Now I go with below approach

现在我采用以下方法

  1. First want to find which type of variable $mois ?(string or integer or other)

  2. Which function is better to find that $mohaving no value.

  1. 首先想找出哪种类型的变量$mo是?(字符串或整数或其他)

  2. 哪个功能更好找,$mo没有价值。

I conduct a test with $moand got these results

我进行了测试$mo并得到了这些结果

is_int($mo);//--Return nothing
is_string($mo); //--Return bool(false) 
var_dump($mo);  //--Return bool(true)                   
var_dump(empty($mo));//--Return bool(true) 
var_dump($mo==NULL);//--Return bool(true) 
var_dump($mo=='');//--Return nothing

Please suggest an optimum and right approach to check the variable integrity

请提出一种最佳且正确的方法来检查变量完整性

回答by Chad

var_dump outputs variables for debugging purposes, it is not used to check the value in a normal code. PHP is loosely typed, most of the time it does not matter if your variable is a string or an int although you can cast it if you need to make sure it is one, or use the is_ functions to check.

var_dump 输出变量用于调试目的,它不用于检查正常代码中的值。PHP 是松散类型的,大多数时候变量是字符串还是整数都没有关系,尽管如果需要确保它是一个,或者使用 is_ 函数来检查,您可以强制转换它。

To test if something is empty:

要测试某物是否为空:

if ( empty( $mo ) ) {
  // error
}

empty() returns true if a variable is 0, null, false or an empty string.

如果变量为 0、null、false 或空字符串,则 empty() 返回 true。

回答by Anthony Forloney

PHP offers a function issetto check if a variable is not NULLand emptyto check if a variable is empty.

PHP 提供了一个函数isset来检查一个变量是否不是NULLempty检查一个变量是否为空。

To return the type, you can use the PHP function gettype

要返回类型,可以使用 PHP 函数 gettype

if (!isset($mo) || is_empty($mo)) {
 // $mo is either NULL or empty.
 // display error message
 }

回答by MANCHUCK

doing strtotime will return false if it cannot convert to a time stamp.

如果无法转换为时间戳,则执行 strtotime 将返回 false。

$mo = strtotime($_POST['MondayOpen']);
if ($mo !== false)
{
//valid date was passed in  and $mo is type int
}
else
{
//invalid date let the user know
}

回答by Sarfraz

You can check its type using:

您可以使用以下方法检查其类型:

gettype($mo);

but nulland emptyare different things, you can check with these functions:

nullempty是不同的东西,你可以用这些函数检查:

if (empty($mo))
{
  // it is empty
}

if (is_null($mo))
{
  // it is null
}

Another way to check if variable has been set is to use the issetconstruct.

检查变量是否已设置的另一种方法是使用isset构造。

if (isset($mo))
{
  // variable has been set
}