php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77928 echo ('string'==0)?"TRUE":"FALSE"; // TRUE
Submitted: 2019-04-20 17:59 UTC Modified: 2019-04-21 15:39 UTC
From: shamil dot kur at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.3.4 OS: Debian
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: shamil dot kur at gmail dot com
New email:
PHP Version: OS:

 

 [2019-04-20 17:59 UTC] shamil dot kur at gmail dot com
Description:
------------
Expected:
echo ('string'==0)?"TRUE":"FALSE"; // FALSE - OK
echo (''==0)?"TRUE":"FALSE";       // TRUE - OK
echo ('string'==1)?"TRUE":"FALSE"; // TRUE - OK
echo (''==1)?"TRUE":"FALSE";       // FALSE - OK

The actual result:
PHP 7.3.4
echo ('string'==0)?"TRUE":"FALSE"; // TRUE - it's ERROR
echo (''==0)?"TRUE":"FALSE";       // TRUE - OK
echo ('string'==1)?"TRUE":"FALSE"; // FALSE - it's ERROR
echo (''==1)?"TRUE":"FALSE";       // FALSE - OK



Test script:
---------------
if(''==0)echo       "printed text 0\n"; //this line printed
if('string'==0)echo "printed text 1\n"; //this line printed  // ERROR
if(''==1)echo       "printed text 2\n"; //never printed      
if('string'==1)echo "printed text 3\n"; //never printed      // ERROR


Expected:
printed text 0
printed text 3

The actual result:
printed text 0
printed text 1



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-04-20 18:22 UTC] spam2 at rhsoft dot net
Expected:
echo ('string'==0)?"TRUE":"FALSE"; // FALSE - OK

fix your expectations!
'string'==0 evaluates to TRUE

php > echo ('string'==0);
1
php > echo ('string'===0);
php >

that's how PHP works all the time 
https://www.php.net/manual/en/language.types.type-juggling.php
 [2019-04-20 18:53 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2019-04-20 18:53 UTC] requinix@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php


 [2019-04-21 06:14 UTC] shamil dot kur at gmail dot com
// You are joking?
// It violates the laws of logic.
$a=0;
$b='string 1';
$с='string 2';

if(($a==$b) && ($a==$c))echo "\$b==\$c";

// But $b don't eq $c.
if($b!=$c)echo "\$b!=\$c";
 [2019-04-21 06:23 UTC] shamil dot kur at gmail dot com
If decided that this is not an error. That does not equate \0 to any string. Let it always be not equal. Because, it is ambiguity.
Is this logical?

<?php
if("any string"==0)echo "this\n";
if("\0"==0)echo "and this\n";

//Result:
this
end this
 [2019-04-21 06:28 UTC] shamil dot kur at gmail dot com
Or do you want to say that when comparing with int, any string == 0? If so, then the question is closed. But "8"==8, "0"==0, "7"==7, "7"!=8 but "7"==0
 [2019-04-21 09:44 UTC] spam2 at rhsoft dot net
what about reading a php documentation for beginners and start using === instead spam bug trackers?
 [2019-04-21 09:48 UTC] spam2 at rhsoft dot net
jesus christ with == one of both needs to be implicit casted before they are compared and a lot of stuff casts to 0 but again: everybody knows that and the documentation is very clear
 [2019-04-21 15:37 UTC] kalle@php.net
This happens due to PHP's nature of type juggling. You are supplying a string on the left operand ('string') and comparing it to an integer on the right operand (0). Please refer to the documentation for this:
https://www.php.net/manual/en/language.types.type-juggling.php
https://www.php.net/manual/en/language.operators.comparison.php


If you supply the same type on both operands you will get the expected results:
https://3v4l.org/Zkind
 [2019-04-21 15:39 UTC] kalle@php.net
You may also find this RFC proposal for PHP8 by Nikita relevant:
https://wiki.php.net/rfc/string_to_number_comparison
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 14:01:30 2024 UTC