$_REQUEST 0 (zero) value = null

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jason.ausmus
Forum Newbie
Posts: 1
Joined: Fri Jul 21, 2006 1:45 pm

$_REQUEST 0 (zero) value = null

Post by jason.ausmus »

Hi,

I'm dealing with a situation where I'm rewriting some crappy old embperl stuff in php, but keeping the UI as unmodified as possible to minimize disruption to the users. The page that I'm having problems with has a large textarea where the user can copy and paste a long list of values from Excel to update a table that joins two other tables on their primary keys.

Whoever designed the lookup tables involved was on crack or something, and there are two situations where the tables have a primary key value = 0. This value is used in a number of places (outside the scope of this application), and apparently is hard-coded in some of them... i.e. I can't just modify the key values for these two records.

As it currently stands, php is interpreting the 0 value as a null, and is failing my validation that checks for the correct number of parameters before inserting.

My question:

Is there a way for me to make this work apart from just making the page assume that a null value = 0 (since this would sort of make the validation I'm doing kind of pointless)?

Thanks,
Jason
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

umm... I think you could use === instead of == in your expression to test for false

Code: Select all

if($bla === false){
    // do something
}
Ward
Forum Commoner
Posts: 74
Joined: Thu Jul 13, 2006 10:01 am

Post by Ward »

Ninja is correct. PHP doesn't really have a true null value. An empty string, a zero, and a boolean false are all treated as boolean false.

For example:

Code: Select all

$value= "";

if (!$value)
{
   // $value evaluates to boolean false
}

$value= 0;
if (!$value)
{
  // $value evaluates to boolean false
}
you can do strict comparisons though, to get around this

Code: Select all

$value = "";

if ($value === 0)
{
   // this will not run, since $value does not strictly-match '0'
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

php has a null value.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

yep... it's NULL :lol: and you test for it with is_null()
Post Reply