简体   繁体   English

如果零浮点值作为字符串传递,则 empty() 返回 true

[英]empty() returns true if a zero float value was passed as string

MySQL with PHP can sometimes be annoying when decimals are returned as strings like with MYSQL functions. MySQL 和 PHP 有时会在像 MYSQL 函数一样以字符串形式返回小数时令人讨厌。 Is there a function to check if a variable is any of these in one?是否有 function 来检查变量是否合二为一?

null, (bool)false, (int)0, (float)0.00, (string)"", (string)"0", (string)"0.00", or (array)[]. null, (bool)false, (int)0, (float)0.00, (string)"", (string)"0", (string)"0.00", or (array)[].

empty() almost does the job but falls on these mysql decimal values sometimes returned as strings. empty()几乎可以完成这项工作,但落在这些 mysql 十进制值上,有时会以字符串形式返回。 isset() and is_null() operates differently in PHP 7 and 8. I am missing something like emptyish(). isset() 和 is_null() 在 PHP 7 和 8 中的操作方式不同。我缺少类似 emptyish() 的东西。

What does not work with empty():什么不适用于空():

if (empty($var)) // Doesn't work for decimal/floats passed as string "0.00"
if (!isset($var) || !$var) // Does not work in PHP 8 for an empty string
if ($var == 0) // Leaves a warning if $var is not declared

This zero/empty condition works the same way in both PHP 7 and 8 versions:这种零/空条件在 PHP 7 和 8 版本中的工作方式相同:

if (empty($var) || (float)$var == 0)

It checks if $var is any of the following: not set, null, (bool)false, (int)0, (float)0.00, (string)"", (string)"0", (string)"0.00", or (array)[]它检查 $var 是否为以下任何一项:未设置、null、(bool)false、(int)0、(float)0.00、(string)""、(string)"0"、(string)"0.00" , 或 (数组)[]

And to substitute empty():并替换为空():

function nil(&$var) { // https://www.dictionary.com/browse/nil
  return (empty($var) || (float)$var == 0);
}

To be used like:像这样使用:

if (nil($var)) {
  echo 'The value is either not set, an empty string, empty array, null, or equals zero.';
}

Check this isset gettype检查这个isset gettype

Isset checking on null and not empty value. Isset 检查 null 而不是空值。 Via gettype you can avoid type string.通过 gettype 你可以避免类型字符串。

if(isset($var) || gettype($v) !== 'string'){
.
.
.
}

use isset().使用 isset()。

<?php
if(isset($var)){
  //$var is set
  if(!$var){
    //$var is 0
  } else if(empty($var)){
    //$var is empty
  } else {
    //code goes here
  }
}else{
  //$var is not set
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM