|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Dec 07 04:00:01 2025 UTC |
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.phpIf 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 thisThis 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