0

I have the following code:

<?php

$starting_id = 0;

$params = array ('val' => $starting_id);

echo parse_params ($params);

function parse_params ($params)
{
    $query = ' WHERE ';
    if ($params['val'] === NULL) {
        $query .= ' IS NULL';
        return $query;
    }
    if ($params['val'] == 'NOT NULL') {
        $query .= ' IS NOT NULL';
        return $query;
    }    
    return $query.' = '.$params['val'];
} 

When I run it, I expect to see this:

WHERE 

instead, I get the following:

WHERE IS NOT NULL

Any ideas why?

EastsideDev
  • 6,257
  • 9
  • 59
  • 116
  • 7
    Because [`'any string' == 0`](http://codepad.org/uodDERp4) is true, you need to do a typesafe check here as well – kero May 13 '15 at 13:06
  • Use `empty` if you want to check 0 and null. http://php.net/manual/en/function.empty.php `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` – chris85 May 13 '15 at 13:12

1 Answers1

1

According to the PHP type comparison tables,

$var = 0 ; // (int)

if you compare $var == "string",

it will return true, you need to type check for this

$var === "string"

check php type comparison

Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
developerCK
  • 4,418
  • 3
  • 16
  • 35