When should you use:
if( //following code
-
!isset($foo) -
!($foo) -
$foo == ""
)
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.
If you don’t use isset and the variable you’re testing is undefined, you’ll generate a warning. Additionally, there is a difference between something having never been set at all, and being set to an empty string, false, null or 0. Consider you’re checking an array key and you just want to know if it’s been created or not – and don’t care about the value – if (!$a[‘key’]) would return false if the key was 0, null or empty.
If you know the variable will be defined and the test only needs to know if the variable is non-false – that is 0, ”, null, or false – then you can bypass isset.