note 128682 added to migration80.incompatible

From: Date: Thu, 06 Jul 2023 12:33:49 +0000
Subject: note 128682 added to migration80.incompatible
Groups: php.notes 
Request: Send a blank email to php-notes+get-220643@lists.php.net to get a copy of this message
If you have older projects that break with PHP7 to 8 migration due to the loose comparison issue: i.e. if ($a == 0) different behaviour between PHP 7 and PHP 8 (for case like $a = "" or $a = "123foo" and other cases listed at top) replace in old code: if ($a == 0) { .. } with if (cmp_eq($a, $b)) { .. } Tested with a wide range of scenarios, even against arrays, booleans, file handles, pipe handles, objects, scalars and numbers. So old code still behave like before. Then both PHP8.x and older PHP up to ver 7.x will give the exact same boolean true or false output for loose comparisons. function cmp_eq($a, $b) { // If both $a and $b are of type strings, compare them as strings if (is_string($a) && is_string($b)) { return $a == $b; } // may not be === because php says '42' equals '042' true yet '42' === '042' is false. // If both $a and $b are numeric strings, compare them as numbers if (is_numeric($a) && is_numeric($b)) { return $a == $b; } // If $a is an empty string and $b is 0, or vice versa, return true if (($a === '' && $b === 0) || ($a === 0 && $b === '')) { return true; } // If $a is a non-numeric string and $b is 0, or vice versa, return true if ((is_string($a) && ($a !== '') && ($b === 0)) || (($a === 0) && is_string($b) && ($b !== ''))) { return true; } // special case '123abc' == 123 .. php 7 casts 123abc to 123, then 123 == 123 results in true. lets mimic that. if ((is_string($a) && ($a !== '') && (is_numeric($b)) && ((bool)$b))) { $number = filter_var($a, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION); //"-234.56xyz" return $number == $b; } if (is_numeric($a) && ((bool)$a) && is_string($b) && ($b !== '')) { $number = filter_var($b, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION); //"-234.56xyz" return $a == $number; } // If $a is a number and $b is a non-numeric string, cast $a to string and compare if (is_numeric($a) && is_string($b)) { return strval($a) == $b; } // If $b is a number and $a is a non-numeric string, cast $b to string and compare if (is_string($a) && is_numeric($b)) { return $a == strval($b); } // If $a and $b are both non-numeric strings, compare them directly, we should return true if they are the same return $a == $b; } // end func cmp_eq Note: the better way would be to port code to PHP 8, use strict variable typing and rather make use of the === and !== operators. But in some cases having lots of old code to quickly patch, this might help. ---- Server IP: 45.112.84.4 Probable Submitter: 45.91.159.168 (proxied: 102.182.66.45) ---- Manual Page -- https://php.net/manual/en/migration80.incompatible.php Edit -- https://main.php.net/note/edit/128682 Del: integrated -- https://main.php.net/note/delete/128682/integrated Del: useless -- https://main.php.net/note/delete/128682/useless Del: bad code -- https://main.php.net/note/delete/128682/bad+code Del: spam -- https://main.php.net/note/delete/128682/spam Del: non-english -- https://main.php.net/note/delete/128682/non-english Del: in docs -- https://main.php.net/note/delete/128682/in+docs Del: other reasons-- https://main.php.net/note/delete/128682 Reject -- https://main.php.net/note/reject/128682 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#220643) next »