William
William

Reputation: 3395

Checking for a null value in conditional in PHP

I have found there to be multiple ways to check whether a function has correctly returned a value to the variable, for example:

Example I

$somevariable = '';

$somevariable = get_somevariable();

if ($somevariable)
{
    // Do something because $somevariable is definitely not null or empty!
}

Example II

$somevariable = '';

$somevariable = get_somevariable();

if ($somevariable <> '')
{
    // Do something because $somevariable is definitely not null or empty!
}

My question: what is the best practice for checking whether a variable is correct or not? Could it be different for different types of objects? For instance, if you are expecting $somevariable to be a number, would checking if it is an empty string help/post issues? What is you were to set $somevariable = 0; as its initial value?

I come from the strongly-typed world of C# so I am still trying to wrap my head around all of this.

William

Upvotes: 0

Views: 2386

Answers (4)

deceze
deceze

Reputation: 522016

It depends on what your function may return. This kind of goes back to how to best structure functions. You should learn the PHP truth tables once and apply them. All the following things as considered falsey:

  • '' (empty string)
  • 0
  • 0.0
  • '0'
  • null
  • false
  • array() (empty array)

Everything else is truthy. If your function returns one of the above as "failed" return code and anything else as success, the most idiomatic check is:

if (!$value)

If the function may return both 0 and false (like strpos does, for example), you need to apply a more rigorous check:

if (strpos('foo', 'bar') !== false)

I'd always go with the shortest, most readable version that is not prone to false positives, which is typically if ($var)/if (!$var).

Upvotes: 1

SirDarius
SirDarius

Reputation: 42879

There is no definite answer since it depends on what the function is supposed to return, if properly documented.

For example, if the function fails by returning null, you can check using if (!is_null($retval)).

If the function fails by returning FALSE, use if ($retval !== FALSE).

If the function fails by not returning an integer value, if (is_int($retval)).

If the function fails by returning an empty string, you can use if (!empty($retval)).

and so on...

Upvotes: 1

Daryl Gill
Daryl Gill

Reputation: 5524

It depends what you are looking for.

Check that the Variable is set:

if (isset($var))
{
 echo "Var is set";
} 

Checking for a number:

if (is_int($var))
{
 echo "Var is a number"; 
}

Checking for a string:

if (is_string($var))
{
 echo "var is a string";
}

Check if var contains a decimal place:

if (is_float($var))
{
 echo "Var is float";
}

if you are wanting to check that the variable is not a certain type, Add: ! an exclamation mark. Example:

if (!isset($var)) // If variable is not set
{
  echo "Var Is Not Set"; 
}

References:

http://www.php.net/manual/en/function.is-int.php

http://www.php.net/manual/en/function.is-string.php

http://www.php.net/manual/en/function.is-float.php

http://www.php.net/manual/en/function.isset.php

Upvotes: 1

Alvaro
Alvaro

Reputation: 41595

If you want to check whether is a number or not, you should make use of filter functions.

For example:

if (!filter_var($_GET['num'], FILTER_VALIDATE_INT)){
    //not a number
}

Upvotes: 0

Related Questions