Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - when zero equals "a" [duplicate]

Tags:

equality

php

I've recently had a bug that was perplexing me.... traced it down to the following (simplified) statmement, which provides a real example

function test_it($team)
{  echo (($team=="a") ? "Yep it is!" : "No Way");
}

So, if I execute test_it(0); I get the result "Yep it is!" ??

The logic of the statement looks fine, but obviously to get a correct outcome I needed to make it "==="....

Can anyone explain why this is the case? Just to help me understand why/what I should avoid in future.

like image 227
Tony Carbone Avatar asked Jun 07 '26 23:06

Tony Carbone


1 Answers

Its because of the string / char of a i.e "a" and passing through 0 .

the === operator performs a 'typesafe comparison'

Explanation and question here

like image 140
Pogrindis Avatar answered Jun 09 '26 12:06

Pogrindis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!