Whey does this evaluate to true?
<?php
$val2=0;
//outputs that is an error123
if($val2=='error123'){
echo 'that is an error123<br />';
}else{
echo 'that is not an error123<br />';
}
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’re comparing a string to an integer. To make the comparison the string is first converted to an integer. When
'error123'is converted to an integer it becomes 0.Result:
In the PHP manual there is an explanation for this behaviour.
There is a quick reference page PHP type comparison tables that shows you the result of various comparions. See the table “Loose comparisons with ==”. The interesting part with regard to this question is that
0 == "php"is shown as TRUE.There is also a page on type juggling. A user comment on that page gives nearly the exact same example as this.
If you don’t want the type juggling use
===instead of==.