Jump to content

Erm, are these the same thing?


newbtophp

Recommended Posts

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..
}
?>

 

8)

Link to comment
https://forums.phpfreaks.com/topic/208876-erm-are-these-the-same-thing/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.