Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP considers P == 0 [closed]

This is absolute insanity. I was testing a post variable today that should always evaluate to a single character. Example code is...

if($_POST['status'] == '' || $_POST['status'] == 0){ die('oh no!'); }

If I pass a status of P, it was executing the die statement. I then created a PHP file with the following code...

echo 'P1: '.intval($_POST['status']=='').'<br />';
echo 'P2: '.intval($_POST['status']==0).'<br />';
echo 'P3: '.intval('P'==0).'<br />';

Guess what? P2 & P3 both evaluate to TRUE. The intval is there just to show 0 instead of nothing on P1.

Is this a known bug of PHP? Is this just something that is broken on the version I am running? Frankly, I'm at a complete loss as to why it is doing this. It evaluates correctly using triple equals, but not on double. P definitely doesn't equal 0 in my book...

like image 930
GameCharmer Avatar asked Apr 18 '26 06:04

GameCharmer


1 Answers

From PHP documentation:

If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically.

like image 130
Tchoupi Avatar answered Apr 19 '26 19:04

Tchoupi