Forum Moderators: coopster

Message Too Old, No Replies

NULL vs 0

how to distinguish NULL from 0 integer

         

briggl

5:27 pm on Apr 5, 2010 (gmt 0)

10+ Year Member



I have a standard HTML form on a web page.
One of the fields (sectionnumber) is defined as type=int with no initial value.
The pseudo code for what I want to do is as follows:

>> if a value is input for sectionnumber, display that section of a file
>> if no sectionnumber is input, display the whole file

A section number of 0 is a valid section number.

If I use "if (empty($sectionnum) === false)" it displays the whole file when 0 is input

If I use "if (is_null($sectionnum) === false)" it only displays section zero when no section number is input

So how do I make this work?

Matthew1980

5:52 pm on Apr 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there briggl,

Welcome to the forum!

The form in question, are you setting an element of this form to hold specifically an int, and if so would it be a floating point int at any time? Reason I ask is because when you submit the form, all elements are set, so you would check them similar to your pseudo code there:-

if(isset($_POST['element_name'] && ($_POST['element_name'] == "")

Essentially isset & empty are the same function, they both take a var and see if it holds anything, even if it nothing-null At least that's how I understand it ;-p


if(empty($_POST['element_name'] && ($_POST['element_name'] == "")

If it could be an int you could force it by doing this:-

$someVar = (int)$_POST['element_name'];
$someVar = (int)$_GET['element_name'];

but if you are expecting an element with a zero value you just do this:

if(isset($_POST['someVar']) && ((int)$_POST['someVar'] == "0" || == 0))

Or something like this, admittedly this is OTF typed, but I think as you are just needing to check to see if an element is set and if its a number. Apologies if I have misunderstood.

[EDIT]To answer the question, Null is nothing ie: == "" and 0 is zero ie: == "0", the "===" means to be identical to, and essentially you are trying to evaluate == "" from == "0". Hopefully I haven't confused you ;-p

Cheers,
MRb

briggl

6:12 pm on Apr 5, 2010 (gmt 0)

10+ Year Member



Yes, it is specified as "type=int" and it will always be just an int.

It seems to me that if
when you submit the form, all elements are set
then isset will always be true, no?

Matthew1980

6:23 pm on Apr 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there briggl,

Yes, upon submission (unless you have an element set to disable), all elements are set, whether they hold value or not, use print_r($_POST); to see what happens when you hit the submit button. This will just give the elements in array format, and any values they hold, even if they are empty.

This is why you use the && ($_POST['somename'] == "") to see if the element holds any value, you can use trim() to get rid of space bar repeats etc. It's Ok to check elements, it's the values they contain that are important ;-p

Cheers,
MRb

Matthew1980

7:33 pm on Apr 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there briggl,

Checking out of curiosity and the input type of int doesn't seem to be supported:-

[w3schools.com ]

So you may want to alter it to type="text", I'm just thinking of form submission compliancy, as I don't think it's wise to define your own input types ;-p

You shouldn't name them int either as this is a reserved word within php.

Just thought as I would mention it.

Cheers,
MRb