php - How does empty() work with a boolean expression? -


a guy learning php sent me code has me scratching head. he's getting $_post input, putting in variables, , then:

if( !empty($id && $name && $email) ) {     //do } 

my first inclination passing multiple variables argument throw error, evaluates successfully. incorrect empty() should not take boolean expression? or - if i'm right - why work?

you can pass expression empty rather variable (since php 5.5), expression lose half of benefit of using empty. empty checks variables set evaluating "truthiness". when give expression that, individual variables within expression not checked exist empty. expression evaluated boolean.

so if used separate empty checks, check variables exist check != false

if(!empty($id) && !empty($name) && !empty($email)) 

but when use

if (!empty($id && $name && $email)) 

you still if block if variables set , have non-false values, you'll undefined variable notices if of them not set. it's same thing not using empty @ all, this:

if ($id && $name && $email) 

but if guy setting these variables $_post, will set, (if weren't in $_post he'd undefined index warnings @ point) , empty here pointless anyway.


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -