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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.