which is better?
if (!empty($val)) { // do something }
and
if ($val) { // do something }
when i test it with PHP 5, all cases produce the same results. what about PHP 4, or have any idea which way is better?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You should use the
empty()construct when you are not sure if the variable even exists. If the variable is expected to be set, useif ($var)instead.empty()is the equivalent of!isset($var) || $var == false. It returns true if the variable is:""(an empty string)0(0 as an integer)0.0(0 as a float)"0"(0 as a string)NULLFALSEarray()(an empty array)var $var;(a variable declared, but without a value in a class)