6

Is there any speed difference between

if (isset($_POST['var']))

or

if ($_POST['var'])

And which is better or are they the same?

Charles
  • 50,943
  • 13
  • 104
  • 142
James
  • 5,942
  • 15
  • 48
  • 72

5 Answers5

17

It is a good practice to use isset for the following reasons:

  • If $_POST['var'] is an empty string or "0", isset will still detect that the variable exists.
  • Not using isset will generate a notice.
Ayman Hourieh
  • 132,184
  • 23
  • 144
  • 116
14

They aren't the same. Consider a notional array:

$arr = array(
  'a' => false,
  'b' => 0,
  'c' => '',
  'd' => array(),
  'e' => null,
  'f' => 0.0,
);

Assuming $x is one of those keys ('a' to 'f') and the key 'g' which isn't there it works like this:

  • $arr[$x] is false for all keys a to g;
  • isset($arr[$x]) is true for keys a, b, c, d and f but false for e and g; and
  • array_key_exists($x, $arr) is true for all keys a to f, false for g.

I suggest you look at PHP's type juggling, specifically conversion to booleans.

Lastly, what you're doing is called micro-optimization. Never choose which one of those by whichever is perceived to be faster. Whichever is faster is so negligible in difference that it should never be a factor even if you could reliably determine which is faster (which I'm not sure you could to any statistically significant level).

cletus
  • 616,129
  • 168
  • 910
  • 942
  • cletus is right - one SQL query will be a thousand times slower than a thousand isset checks. It's also worth noting that if($arr['g']) will generate an E_NOTICE "Undefined variable". – Shabbyrobe May 10 '09 at 22:56
4

isset tests that the variable has any value, while the if tests the value of the variable.

For example:

// $_POST['var'] == 'false' (the string false)
if (isset($_POST['var'])) {
    // Will enter this if
}
if ($_POST['var']) {
    // Won't enter this one
}

The big problem is that the equivalency of the two expressions depends on the value of the variable you are checking, so you can't make assumptions.

pgb
  • 24,813
  • 12
  • 83
  • 113
  • I have a feeling you need a little correction on the first line. isset doesn't "test that the variable has any value" it tests if it has been instantiated/created `$a = ''; isset($a);` will evaluate to true. – Fernando Silva Jul 24 '14 at 10:10
1

In strict PHP, you need to check if a variable is set before using it.

error_reporting(E_ALL | E_STRICT);

What you are doing here

if($var)

Isn't checking if the value is set. So Strict PHP will generate a notice for unset variables. (this happens a lot with arrays)

Also in strict PHP (just an FYI for you or others), using an unset var as an argument in a function will throw a notice and you can't check isset() within the function to avoid that.

Ólafur Waage
  • 68,817
  • 22
  • 142
  • 198
  • This is not completely correct. E_STRICT has nothing to do with a notice being thrown in this case. Even if E_STRICT is off, a notice is still thrown. – Matt May 10 '09 at 03:07
  • You mean in code or log file? Because you need to enable E_STRICT in the php.ini for it to be thrown (pre PHP 6) – Ólafur Waage May 10 '09 at 08:37
0

Just repeating what others said, if you execute:

if($variable)

and $variable is not set, you'll get a notice error. Plus..

$var = 0;
if($variable) {
    //This code will never run, because $var is false
}

but using isset would return true in this case.

Daniel Sorichetti
  • 1,921
  • 1
  • 20
  • 34