Note - I know pretty much about empty() and isset(), what I am asking is the most proper/efficient way of using them.
I've just saw at php.net this sentence under empty()
reference:
No warning is generated if the variable does not exist. That means empty() is essentially the concise equivalent to !isset($var) || $var == false.
But again, as I'm mainly working now on other people's apps (either standalone projects, websites using cms or even frameworks) many many times I've seen people writing:
if (isset ($var) && $var != '')
// or
if (isset ($var) && !empty($var))
(mainly with $_GET and $_POST variables, but not only)
As far as I understand manual, snippet below is equivalent:
if (!empty($var))
Or am I missing something? What is technically the best way to check existense of variable with value?
I know both functions, their destinations. I just want to know if empty()
is all needed to check if is variable set and given value. I know that it works, but is it 100% proper and safe?
empty()
is preferred way – Harrisharrisburgisset
andstrlen
because 0 will return true for empty. – Ascoisset($v) && $v
is full equivalent of!empty($v)
– Berl$var = '0'
,var_dump(isset ($var) && $var != '')
is not the same asvar_dump(!empty($var))
. You should be careful when comparing strings with other types. – Makkah