Zero considered as empty in PHP
Asked Answered
C

6

5

I've got a problem with an input field in my php form. It looks like:

<input type="number" name="tmax" max="99" min="-99" placeholder="Temperatura max."> 

I want to check whether the field is empty or not. But the problem is php considers 0 as empty.

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

If the user leaves the input field empty the value is considered as 'null', which works perfectly. But if the user writes 0, which is a possibility in the form, it is also treated as empty.

I've also set the default as null in SQL but the problem is, if the input is empty, the program inserts 0 in the table.

SOLUTION:

This solution works fine for me:

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

And also with is_numeric()

if (is_numeric($_POST['tmax'])) {
  $tmax = $_POST['tmax'];
}else {      
    $tmax = 'null';
}
Cointon answered 29/7, 2017 at 15:41 Comment(1)
Try to create a custom statement when the degree is 0Folkway
A
0

you may use

if ($_POST['tmax'] == "") {
  $tmax = null;
}else {
  $tmax = $_POST['tmax'];
}
Archegonium answered 29/7, 2017 at 15:52 Comment(2)
This will throw notices.Weylin
See lucianov's better answer, which includes !isset to avoid the php warning chris mentions. Or any other answer below (except Lesiuk's, which is quite good for a different situation, but not what is asked for here).Logy
P
4

Check if the condition is empty, and also not zero. A zero-value is "empty", so by adding both checks, you ensure that the variable $tmax will be set to null if the input was empty and not zero.

if (empty($_POST['tmax']) && $_POST['tmax'] != 0) {
    $tmax = null;
} else {
    $tmax = $_POST['tmax'];
}

This will also accept "foo" as a value, so you should check or validate that the input is a valid number (and also in the ranges you specified). You can also implement is_numeric($_POST['tmax']), or even better, validate it with filter_var($_POST['tmax'], FILTER_VALIDATE_INT) to ensure that whatever was input is actually a number.

Phrixus answered 29/7, 2017 at 15:50 Comment(1)
IMHO, its a confusing technique to use empty, then add additional tests to correct for emptys "too inclusive" design - if its a situation where empty doesn't do what is wanted, don't use it, in my opinion. I far prefer to build up from more fundamental calls, that have simpler semantics. You mention is_numeric - that seems the better starting point.Logy
K
2

You can use !is_numeric() instead empty()

thanks for such a important note, Rafa

Kaufmann answered 29/7, 2017 at 15:49 Comment(1)
Note: To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric(). But thanks a lot! its work for me.Cointon
P
1

As you state, 0 is considered empty.

The function you want is isset().

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

Alternatively, remove the not operator and switch the code blocks.

Plowman answered 29/7, 2017 at 15:49 Comment(0)
K
1

This code should work for what are you trying to get.

if (!isset($_POST['tmax']) || $_POST['tmax'] == '') {
    $tmax = null;
}else {
    $tmax = $_POST['tmax'];
}
Kahlil answered 29/7, 2017 at 15:55 Comment(0)
S
1

if you want to have placeholder - you can use this code:

<input type="number" name="tmax" max="99" min="-99" onclick="if (this.value == '') {this.value='0';} " placeholder="Temperatura max.">

don't forget add validation (before send form check on empty fileds )

and php to:

$tmax = 0;
if (isset($_POST['tmax'])) {
  $tmax = $_POST['tmax'];
}
Smithereens answered 29/7, 2017 at 16:11 Comment(3)
Thanks for the reply, but this solution is not esthetic for me, because the info is displayed on placeholder, and with 'value' the user can see the info.Cointon
Ok I change answer , now - with placeholderSmithereens
This is a great technique if you want to replace an empty field with a default value. [To clarify for future readers: Original question wanted the opposite behavior - how to distinguish between an empty value, which means "not set", and is permitted in his code, and a value of zero.]Logy
A
0

you may use

if ($_POST['tmax'] == "") {
  $tmax = null;
}else {
  $tmax = $_POST['tmax'];
}
Archegonium answered 29/7, 2017 at 15:52 Comment(2)
This will throw notices.Weylin
See lucianov's better answer, which includes !isset to avoid the php warning chris mentions. Or any other answer below (except Lesiuk's, which is quite good for a different situation, but not what is asked for here).Logy

© 2022 - 2024 — McMap. All rights reserved.