In PHP, Is 0 Treated as Empty

In php, is 0 treated as empty?

http://php.net/empty

The following things are considered to be empty:

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

Note that this is exactly the same list as for a coercion to Boolean false. empty is simply !isset($var) || !$var. Try isset instead.

Zero considered as empty in PHP

you may use

if ($_POST['tmax'] == "") {
$tmax = null;
}else {
$tmax = $_POST['tmax'];
}

0' as a string with empty() in PHP

You cannot make empty() take it. That is how it was designed. Instead you can write an and statement to test:

if (empty($var) && $var !== '0') {
echo $var . ' is empty';
}

You could use isset, unless of course, you want it to turn away the other empties that empty checks for.

PHP: what's an alternative to empty(), where string 0 is not treated as empty?

The answer to this is that it isn't possible to shorten what I already have.

Suppressing notices or warnings is not something I want to have to do, so I will always need to check if empty() or isset() before checking the value, and you can't check if something is empty() or isset() within a function.

How to differentiate a value is zero or NULL in php?

to check for null use :

if (is_null($moon))

to check for integer 0 use:

if ($moon === 0)

to check for integer 0 or string "0"

if ($moon == '0')

I have to add that php will consider any string equals with number 0 when using "==":

$moon = "asd";

if ($moon == 0) // will be true

How to treat zero values as true using php?

The last is okay, meaning if I pass any value having ZERO then it echo true. But it also echo true if I don't pass any value. Any idea?

if (isset($_POST["issue_id"]) && $_POST["issue_id"] !== "") {
}

please notice I used !== not !=. this is why:

0 == "" // true
0 === "" // false

See more at http://php.net/manual/en/language.operators.comparison.php


also if you are expecting number you can use

if (isset($_POST["issue_id"]) && is_numeric($_POST["issue_id"])) {
}

since is_numeric("") returns false

http://php.net/manual/en/function.is-numeric.php


Alternatively if you expect number good option is filter_var

if (isset($_POST["issue_id"]) {
$issue_id = filter_var($_POST["issue_id"], FILTER_VALIDATE_INT);
if ($issue_id !== false) {
}
}

since filter_var("", FILTER_VALIDATE_INT) will returns false and filter_var("0", FILTER_VALIDATE_INT) will return (int) 0

http://php.net/manual/en/function.filter-var.php

Check Empty and zero on PHP

change this line

 if (empty($num1 AND $num2)){}

to

if (isset($num1) && isset($num2)  && $num1>=0 && $num2>=0){}// this will show warning if $num1 or $num2 is not set

or

if (!empty($num1) && !empty($num2)  && $num1>=0 && $num2>=0){}// this will not show warnings

empty will consider 0 as empty but if $num1 or $num2 is not set already this will not show any error

Null vs. False vs. 0 in PHP

It's language specific, but in PHP :

Null means "nothing". The var has not been initialized.

False means "not true in a boolean context". Used to explicitly show you are dealing with logical issues.

0 is an int. Nothing to do with the rest above, used for mathematics.

Now, what is tricky, it's that in dynamic languages like PHP, all of them have a value in a boolean context, which (in PHP) is False.

If you test it with ==, it's testing the boolean value, so you will get equality. If you test it with ===, it will test the type, and you will get inequality.

So why are they useful ?

Well, look at the strrpos() function. It returns False if it did not found anything, but 0 if it has found something at the beginning of the string !

<?php
// pitfall :
if (strrpos("Hello World", "Hello")) {
// never exectuted
}

// smart move :
if (strrpos("Hello World", "Hello") !== False) {
// that works !
}
?>

And of course, if you deal with states:

You want to make a difference between DebugMode = False (set to off), DebugMode = True (set to on) and DebugMode = Null (not set at all, will lead to hard debugging ;-)).

$_POST not getting number zero

To explain why this is happening, you have to take a closer look at the PHP empty() function. From the documentation we see that (emphasis mine)...

Determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value equals FALSE.

In PHP, the value of zero is a falsy value. So you get the following output,

var_dump(false == 0); // Outputs "true"
var_dump(empty(0)); // Outputs "true"

So you need to alter your code to check if the variable is set and greater or equal to zero, which you can do by altering the snippet you showed to the following.

if (isset($_POST['revisionCad']) && $_POST['revisionCad'] >= 0) {
$revisionCad = $_POST['revisionCad'];
} else {
erDisplay('Please select a revision for Cadgui');
}
  • http://php.net/empty

How can I prevent my function from treating 0 as empty?

You're correct that empty() returns TRUE for 0.

You could instead use filter_input() with the FILTER_VALIDATE_INT filter applied.

You could even simplify the whole thing with a range map...

$str = implode("", array_map(function($i) {
$val = filter_input(
INPUT_POST,
"n_$i",
FILTER_VALIDATE_INT
);

return $val === null || $val === false
? '.'
: $val;
}, range(1, 6)));


Related Topics



Leave a reply



Submit