php - Correct Way to determine if function is set -
i cant seem find solution issue. im trying check if value of function has been set , if has stuff when run below codes bottom 1 throws error code @ bottom code wish use
seems pointless calling function variable see if set
//working check if set $wa_passwordreset = wa_fn_validate_post($_post['wa_passwordreset']); if(isset($wa_passwordreset)) { echo"do stuff"; } //not working check if set if(isset( wa_fn_validate_post($_post['wa_passwordreset']) )) { echo"do other stuff"; }
as per php manual:
isset — determine if variable set , not null
isset test if variable set, not if function returns value. why code doesn't work.
im trying check if value of function has been set
functions not have values. functions may return values. can't check if function has value , if it's set.
moreover,
$wa_passwordreset = wa_fn_validate_post($_post['wa_passwordreset']); if(isset($wa_passwordreset)) { echo "do stuff"; } this portion of code always return true. reason if wa_fn_validate_post return empty string, variable $wa_passwordreset considered set , isset check return true. avoid this, should either check $_post this:
if(isset($_post['wa_passwordreset'])) { $wa_passwordreset = wa_fn_validate_post($_post['wa_passwordreset']); echo "do stuff"; } or if it's vital maintain order , check after wa_fn_validate_post, use empty:
$wa_passwordreset = wa_fn_validate_post($_post['wa_passwordreset']); if(isset($wa_passwordreset) && !empty($wa_passwordreset)) // paranoid! { echo "do stuff"; }
Comments
Post a Comment