PHP Comparison Issues ... 0 equates to 'D'

Bug #928550 reported by LaChild
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
php5 (Ubuntu)
Invalid
Undecided
Unassigned

Bug Description

Hello,

Using "PHP 5.3.6-13ubuntu3.3 with Suhosin-Patch (cli)" I noticed the following bug. Apparently the (integer) 0 equates to the same thing as (string) 'D'

Example:
----------------------------------------------
<?php
$test = 'D';

if ($test == 'D')
echo 'true'; // Echos true

$testZero = 0;

if ($testZero == 'D')
echo 'true'; //Also echos true
----------------------------------------------

Both equate to "true" but the 0 should not.

Thanks,
LaChild

Tags: php php5
Revision history for this message
Steve Beattie (sbeattie) wrote :

Thanks for taking the time to report this issue and help improve Ubuntu. While from a programmer's perspective, it's unexpected behavior; however, it is correct as documented at:

  http://php.net/manual/en/language.operators.comparison.php

What's happening is that when comparing a string to a number, php is converting the string (in this case 'D') to a number. Because 'D' isn't a (base 10) number, it treats it as the number 0. And thus 0 == 0 returns true.

Please feel free to report any additional issues you come across. Thanks again!

Changed in php5 (Ubuntu):
status: New → Invalid
Revision history for this message
LaChild (shafer-w2002) wrote :

Wow, thanks.... Guess I just never hit this one before... I wonder how many bugs are in my scripts because of this... doh!

Thanks for the quick the reply.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.