PHP: is empty($var) equivalent to !$var -


for validating variable value can

if(empty($var)){ } 

or

this return true on empty string, 0 number, false, null

if(!$var){ } 

what difference between 2 approaches, them equivalent?

edit: examples behave different more pratical.

edit2: difference founded answers second throw notice if $var undefined. boolean value return?

edit3: $var mean variable value, or undefined variable.

conclusion users answers: if(!$var) , empty($var) equivalent described here http://php.net/manual/en/types.comparisons.php, return same bool value on same variable.

if(!$var) return php notice if $var not defined, not case (if write code) ides underline it.

  • when checking simple variables if(!$var) should ok
  • when checking arrays index ($var['key']) or object properties ($var->key) empty($var['key']) better using empty.

the problem since !$vars shorter empty($vars) many of prefer first way

you prefer first 1 because "shorter way"?
shorter code not mean better code, or faster scripts!

the speed of php functions , various other behaviours not determined length of function name. determined php doing evaluate, action, , return results.

besides that, don't choose methods based on length of code, choose methods based on scenario , best approach "for given scenario".
best depends on need, , there other variable checks other 2 mentioned (isset() one).

but problem equivalent

no, , there plenty of other answers on stack, google, , php.net - aka php manual!
http://php.net/manual/en/types.comparisons.php

or, create quick test script see php returns 2 scenarios.

all of why wondered why there 6 upvotes...

adding that, initialising variables in framework (or, likely, stand alone script), means scenario changes, question , approach use.

it's contextual best.

as required answer.

anyway, answer question, here tests:

(!$vars)

example code:

if ( !$vars )  {   echo "true";  } else  {   echo "false";  } 

will return:
notice: undefined variable: vars in /whatever/ on line x
true

however, if initialise var in scripts somewhere first:

$vars = ""; $vars = null; $vars = 0; 

any of above return:
[no php notice]
true

$vars = "anything"; 

will return:
false

this because exclamation mark testing if var false, when not initialised string test script returns true because not false.

when initialise string var not being false false.

empty($vars)

example code:

if ( empty($vars) )  {   echo "true";  } else  {   echo "false"; } 

not initialised/set @ all, , of following:

$vars = ""; $vars = null; $vars = 0; 

will return:
true

there no php notice using empty, here show difference between 2 options (and remember when said shortest code not "best"? depends on scenario etc.).

and previous test:

$vars = "anything"; 

returns:
false

this same ( !$var ), testing if empty, , without var being initialised @ all, or "empty()" value: eg (""), or null, or 0 (0), testing if var empty true, true output.

you false when setting var string because empty = false set something.


the difference empty() not throw php notice when var not defined, whereas (!$var) will.

also, may prefer being "shorter code", think if ( !$var ) looks ugly, , isn't simple view if ( empty($var) ).
again, depends on scenario - php provides different options suit different requirements.


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -