How does the “empty” function in PHP filter out the interference caused by 0?

In PHP, we can use the empty() function to check if a variable is empty. However, the criteria for empty() is quite loose, as it considers the following conditions as empty variables:

  1. Both a variable equal to 0 or a variable equal to the string “0” will be considered empty.
  2. If the variable is an empty string “”.

    If the variable is contained within empty quotation marks “”.

  3. If the variable is NULL.
  4. If the variable is an empty array.

Therefore, if you want to filter out the interference of zero, you can use other more strict conditions to determine if the variable is empty. Here are several common practices:

  1. He found it difficult to keep up with the fast pace of the conversation.
if ($var === 0) {
    // 变量为0
} else {
    // 变量不为0
}
  1. remove blank spaces from (a string)
if (trim(strval($var)) == "") {
    // 变量为空
} else {
    // 变量不为空
}
  1. This function checks if a value is a number.
if (is_numeric($var) && $var != 0) {
    // 变量为非零数字
} else {
    // 变量为空或为0
}

Choose the appropriate method to filter out the interference of zeros based on specific needs.

bannerAds