newbtophp Posted July 26, 2010 Share Posted July 26, 2010 I have a function validate() which returns true or false, and I was wanting to know if the followin 3 if examples all do the same thing; and which is the best or your preference to go by doing this? <?php if (validate($input)) { //etc.. } //is the same as the following? if (validate($input) == true) { //etc.. } //is the same as the following? if (validate($input) == 1) { //etc.. } ?> Quote Link to comment https://forums.phpfreaks.com/topic/208876-erm-are-these-the-same-thing/ Share on other sites More sharing options...
AbraCadaver Posted July 26, 2010 Share Posted July 26, 2010 Depends on what your function returns, but they are the same in general if your function returns true: The first one tests if the return evaluates to a non false value (not false, 0, '', null) the second does a loose comparison with true (same as previous one), the last one is a loose comparison so 1 == true, but if you return 2 or 3, etc... then it would fail obviously, even though they evaluate to true they don't equal 1. If you are returning true or false then the first one is fine, however you may not always consider things that evaluate to false as false. One example being strpos(), where strpos('X', 'X') is 0 (the position), but is not false because the string was found. Quote Link to comment https://forums.phpfreaks.com/topic/208876-erm-are-these-the-same-thing/#findComment-1091070 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.