views:

255

answers:

5

Hi, just trying to optimize my code. I need to prefill a form with DB data and I need to check if the variable exist to fill the text box (I dont like the @ error hiding). The form is really long, then I need to check multiple times if the variables exist.

Then what is faster:

  • if (isset ($item))
  • if ($item_exists==true)

or even

  • if ($item_exists===true)
+5  A: 

I'm sure there is a performance difference; I'm sure it has been benchmarked somewhere; but I'm also sure it really doesn't matter at all for real-world purposes. Any achievable gain is in the milliseconds here, and what's much, much more important is the readability of the code, and the avoiding of warnings (which cost performance, whether output or not).

You will probably need isset if you can't be sure it's set at the time you access it. type-safe comparison === shouldn't be necessary if $item_exists always is a boolean, but it won't harm either. So depending on your situation, you may need

if ((isset($item)) and ($item_exists === true))
Pekka
Thanks for your answer Pekka.
earlyriser
+8  A: 

In this case you shouldn’t ask about performance but about correctness first. Because isset does not behave like a boolean convertion and comparison to true (see type comparison table). Especially the values "" (empty string), array() (empty array), false, 0, and "0" (0 as a string) are handled differently.

Gumbo
+1. On top of the difference in comparison tables, using a comparison operator on an undeclared variable results in an E_NOTICE.
Frank Farmer
+1  A: 

To answer though, most likely isset() and "===" will be fastest as they only check for one condition, where == checks for multiple conditions and be slower. I haven't officially tested this, but I think its right. @Pekka is also correct, if you are looking to optimize, these really aren't going to be where you do it. As it would probably take thousands of calls just to notice a few milliseconds of difference.

ocdcoder
+6  A: 

with a for cicle 10000000 times the same script took:

  • if (isset ($item)) 2.25843787193
  • if ($item_exists==true) 6.25483512878
  • if ($item_exists===true) 5.99481105804

so I can tell the isset surely is faster...

Marcx
+1 for doing the benchmarking. It also shows that it makes no real difference - it's extremely rare for a PHP script to do something 10 million times, and usually, the database / file I/O overhead would massively increase the time needed even then.
Pekka
what was $item_exists? I think the "==" might vary based on its type
ocdcoder
To add to the numbers game: Either way is on the order of 0.0000001 seconds. Which means you'd have to do it around 1,000,000 times per page to be something the user would notice (assuming 0.1 seconds is what the user would notice).In short, the sort of micro-optimization that will rarely, if ever, make a difference.
Jim Leonardo
This is awhul performance related question and awful answersuch trifle things has nothing common with **optimization**the only Pekka's answer makes sense
Col. Shrapnel
Thanks for testing it Marcx.
earlyriser
+1  A: 

There is only one way to optimize your code, called "profiling". First you got to know what part of code requires optimization. And only then run tests, find solutions, etc.

The "circling" approach from the Marcx's answer is awful too. if you want to test if any code has real difference, test it from the browser's point of view, using apache benchmark utility.

Col. Shrapnel