Is php 0 being treated as empty?

The code will explain more:

$var = 0; if (!empty($var)){ echo "Its not empty"; } else { echo "Its empty"; } 

The result returns "Empty". I thought empty () checks to see if I have already set the variable and have a value inside. Why does he return "Empty"?

+117
function php
Feb 08 '10 at 9:16
source share
18 answers

http://php.net/empty

The following things are considered empty:

  • "" (empty line)
  • 0 (0 as an integer)
  • 0.0 (0 as a floating point number)
  • "0" (0 as a string)
  • ZERO
  • FALSE
  • array () (empty array)
  • var $ var; (declared variable, but no value in the class)

Note that this is exactly the same list as for casting to logical false . empty is easy !isset($var) || !$var !isset($var) || !$var !isset($var) || !$var !isset($var) || !$var . Try instead isset .

+159
08 Feb '10 at 9:18
source share
β€” -

I was wondering why no one suggested an extremely convenient type comparison table . It answers every question about common functions and compares operators.

Excerpt:

 Expression | empty() ----------------+-------- $x = ""; | true $x = null | true var $x; | true $x is undefined | true $x = array(); | true $x = false; | true $x = true; | false $x = 1; | false $x = 42; | false $x = 0; | true $x = -1; | false $x = "1"; | false $x = "0"; | true $x = "-1"; | false $x = "php"; | false $x = "true"; | false $x = "false"; | false 

On top of all the cheat sites, I always keep a hard copy of this table on my desk if I'm not sure.

+68
Feb 08 '10 at 9:30
source share

In the case of numeric values, you should use the is_numeric function:

 $var = 0; if (is_numeric($var)) { echo "Its not empty"; } else { echo "Its empty"; } 
+31
May 29 '13 at 7:22
source share

Use strlen() . I ran into the same problem using 1/0 as possible values ​​for some variables. I use if (strlen($_POST['myValue']) == 0) to check if there is a character or not in my variable.

+10
Aug 25 '14 at 11:09
source share

Recently I was caught with my pants on this one too. The problem that we often encounter is not defined variables - for example, a form element that may or may not be there, but for many elements 0 (or the string '0' that will go through the message more accurately, but still will be evaluated as "false") is the legal value specified in the drop-down list.

using empty() first and then strlen() is your best if you need it as well:

 if(!empty($var) && strlen($var)){ echo '"$var" is present and has a non-falsey value!'; } 
+8
Nov 10 '15 at 12:21
source share

From a linguistic point of view, empty matters without meaning. Like others, you'll need to use isset() to check if a variable has been defined, which is what you do.

+6
Feb 08 '10 at 9:32
source share

It works for me!

 // if(isset($_POST['post_var'])&&$_POST['post_var']!==''){ echo $_POST['post_var']; } //results: 1 if $_POST['post_var']='1' 0 if $_POST['post_var']='0' skip if $_POST['post_var']='' 
+4
Aug 11 '17 at 6:37
source share

empty() returns true for everything that evaluates to FALSE , in fact it is "no" ( ! ) in disguise. I think you meant isset()

+3
Feb 08 '10 at 9:19
source share

To accept 0 as a value in a variable, use isset

Check if the variable is empty

 $var = 0; if ($var == '') { echo "empty"; } else { echo "not empty"; } //output is empty 

Check if the variable is set

 $var = 0; if (isset($var)) { echo "not empty"; } else { echo "empty"; } //output is not empty 
+3
May 18 '17 at 9:23 a.m.
source share

The following things are considered empty:

  • "" (empty line)
  • 0 (0 as an integer)
  • "0" (0 as a string)
  • Null
  • False
  • array () (empty array)
  • var $ var; (variable declared, but no value in class)

From PHP Guide

In your case, $var is 0 , so empty($var) will return true , you negate the result before testing it, so the else block will start with the output of " Its empty " as output.

+1
Feb 08 '10 at 9:19
source share

From the manual: Returns FALSE if var has a non-empty and non-zero value.

The following things are considered empty:

  • "" (empty line)
  • 0 (0 as an integer)
  • "0" (0 as a string) NULL
  • FALSE array () (empty array) var
  • $ var; (variable declared, but no value in class)

More details: http://php.net/manual/en/function.empty.php

+1
Feb 08 '10 at 9:20
source share
 if (empty($var) && $pieces[$var] != '0') { //do something } 

In my case, this code worked.

+1
Jun 26 '14 at 8:27
source share

In fact, just check if the variable is set or not. In this case, if you want to check if your variable is really null or empty, you can use this example:

 $myVar = ''; if (empty($myVar)) { echo "Its empty"; } echo "<br/>"; if ($myVar===0) { echo "also zero!"; } 

for notification only, $ myVar == 0 acts as an empty function.

0
Mar 08 '11 at 7:17
source share

empty should mean empty.

When i do

 $var = ''; $var = '0'; 

I expect var_dump(empty($var)); will return false.

if you are checking things in an array, you always need isset($var) .

0
Oct 13 '17 at 18:07 on
source share

use only ($_POST['input_field_name'])!=0 instead !($_POST['input_field_name'])==0 then 0 is not considered empty.

0
Jan 09 '18 at 16:35
source share

Not sure if there are still people who are looking for explanations and solutions. The comments above say all about the differences between TRUE / FALSE / 1/0.
I just would like to bring my 2 cents for the way the actual value is displayed.

BOOLEAN

If you are working with a logical data type, you are looking for the result TRUE or FALSE; if you store it in MySQL, it will be stored as 1 resp. 0 (if I'm not mistaken, this is the same in the memory of your server).

Thus, in order to display the value in PHP, you need to check whether it is true (1) or false (0), and display everything you want: TRUE or FALSE or, possibly, 1 or 0 ".
Attention, anything that is larger (or different) from 0 will also be considered TRUE in PHP. For example: 2, "abc", etc. Will return TRUE.

BIT, TINTINT

If you are working with a numeric data type, the way it is stored is the same.
To display a value, you must tell PHP to treat it as a number. The easiest way I've found is to multiply it by 1.

0
Feb 17 '19 at 19:55
source share

right example. just create an int field (like a mobile phone number) in the database and send an empty value for the next database via the form or simply paste using SQL. that it will be stored in database 0 because it is of type int and cannot be stored as empty or null. therefore, it is empty, but will be saved as 0. Therefore, when you retrieve data through PHP and check for empty values. it is very useful and logically correct.

0,00, 0,000, 0,0000 .... 0,0 ... 0 is also empty, and the above example can also be used to store various types of values ​​in the database, such as float, double, decimal (decimal have different options like 0,000 and so on).

0
Mar 06 '19 at 6:38
source share

You need to use isset () to check if the value is set.

0
May 15, '19 at 4:22
source share



All Articles