On FILTER VALIDATES Which One To Use: '===' or '=='?

Hello Php People,

Which one you favour and why that over others ?

if(filter_var("https://www.sitepoint.com", FILTER_VALIDATE_URL) !== false) {
  // it's good url
}

If you want to test if it’s not valid, then:

if(filter_var("https://www.sitepoint.com", FILTER_VALIDATE_URL) === false) {
  // it's bad url
}

Is this following statement true or not on this subject ?
“Using the type-explicit === and !== comparison operators avoids treating empty strings or something like “0” as being false.” ?

https://www.php.net/filter_var

I usually do it like following and so they not ok ?

if(!filter_var("https://www.sitepoint.com", FILTER_VALIDATE_URL)) {
  // it's bad url
}
if(filter_var("https://www.sitepoint", FILTER_VALIDATE_URL)) {
  // it's good url
}

They not best than the previous two ? Yes or no ? I’d like to know. From you.

Let’s see how you code it.

Thank you in advance for your upcoming feed backs.

General rule, you should always try to use === for your comparisons. This is because this is going to compare not just on value but on type. And yes, it makes sure that something like an empty string is treated as the same as being false or 0 because the string is not equal to a boolean or an integer.

Again notice that we are comparing type of value not just the value when we use the triple equals. :slight_smile:

1 Like

Thread closed as the OP is banned.