I'm pretty sure this is a simple fundamental flaw in my newb PHP knowledge, but I was surprised when the following happened:
$result
is TRUE
... so why is it considered equal to the string "email"? I'm guessing this is because, technically, it's a bool
and it isn't false? So when it's compared against a string (e.g. "email") it returns true.
Should I change my method to return as the result as a string
containing "true" (instead of return true;
on success), or is there another way I should be doing this?
Thanks.
Control Statements. Boolean expressions. Compare strings. In Java, syrings are compared character by character, starting with the first character and using the Unicode collating sequence.
Using the Strict Equality Operator (===) In this method, we will use the strict equality operator to compare strings to Boolean. The strict equality always returns false when we compare string and boolean values as it also checks for the data type of both operands.
Boolean values are values that evaluate to either true or false , and are represented by the boolean data type. Boolean expressions are very similar to mathematical expressions, but instead of using mathematical operators such as "+" or "-", you use comparative or boolean operators such as "==" or "!".
You can use the boolean operators "==" and "! =" to compare two strings. You can use the "==" operator to test strings for similarity and the "! =" operator to check strings for inconsistencies.
Yes, true
is equal (==
) to a non-empty string. Not identical (===
) though.
I suggest you peruse the type comparison table.
It returns true because php will try to convert something to be able to compare them. In this case it probably tries to convert the string on the right side to a bool which will be true in this case. And true == true is ofcourse true.
By doing $result === "email" (triple =) you tell PHP that it shoudn't do conversions and should return false if the types don't match.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With