I am just trying to optimize my code.
I need to prefill a form with data from a database, and I need to check if the variable exist to fill the text box (I don't like the @
error hiding).
The form is really long, then I need to check multiple times if the variables exist.
What is faster of the following two?
if (isset ($item))
if ($item_exists==true)
Or even
if ($item_exists===true)
PHP's in_array() function is really slow.
isset( $_POST['submit'] ) : This line checks if the form is submitted using the isset() function, but works only if the form input type submit has a name attribute (name=”submit”).
Definition and Usage. The isset() function checks whether a variable is set, which means that it has to be declared and is not NULL. This function returns true if the variable exists and is not NULL, otherwise it returns false.
The isset() and ! empty() functions are similar and both will return the same results. But the only difference is ! empty() function will not generate any warning or e-notice when the variable does not exists.
With a for
loop repeating it 10000000 times the same script took:
if (isset ($item))
2.25843787193if ($item_exists==true)
6.25483512878if ($item_exists===true)
5.99481105804So I can tell isset
surely is faster.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With