服务器需要PHP严格比较,但不需要在localhost上

I've run across a weird issue with comparison operators.

On my localhost this code runs fine:

$variable = 2;
if($variable == 2){
  echo 'hi';
}
if($variable == '2b'){
  echo 'bye';
}
//returns 'hi'

But on my server, it returns 'bye', unless I use the strict (===) operator.

Is there any way to change this so my server also returns 'hi'?

This is because php try to cast (int) to your string because you compare an int with a string so your if block would be

if($variable == (int) '2b'){

So when casting to it, so (int) '2b' would be only 2. If you use === it works cause of, that is also check if it is the same type.

I've had a similar situation in the past. It's silly, but make sure you are running the same PHP version in both servers.

Either way, your production server works as it should, but your local server doesn't. Test:

echo 2 == '2b' ? 'true' : 'false'; // true
echo 2 == 'b2' ? 'true' : 'false'; // false

A string will be 'casted' into a number if it starts with a number.