<?php
class Php8CompatUtils
{
public static function eq($a, $b): bool
{
// 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 (is_array($a) || is_array($b)) {
return static::standardArrayCompare($a, $b) == 0;
}
// If $a and $b are both non-numeric strings, compare them directly, we should return true if they are the same
return $a == $b;
}
public static function gt($a, $b): bool
{
if (static::eq($a, $b)) {
return false;
}
if (is_array($a) || is_array($b)) {
return static::standardArrayCompare($a, $b) < 0;
}
return $a > $b;
}
public static function lt($a, $b): bool
{
if (static::eq($a, $b)) {
return false;
}
if (is_array($a) || is_array($b)) {
return static::standardArrayCompare($a, $b) > 0;
}
return $a < $b;
}
public static function gte($a, $b): bool
{
if (is_array($a) || is_array($b)) {
return static::standardArrayCompare($a, $b) >= 0;
}
return static::eq($a, $b) || ($a > $b);
}
public static function lte($a, $b): bool
{
if (is_array($a) || is_array($b)) {
return static::standardArrayCompare($a, $b) <= 0;
}
return static::eq($a, $b) || ($a < $b);
}
private static function standardArrayCompare($op1, $op2)
{
if (count($op1) < count($op2)) {
return -1; // $op1 < $op2
} elseif (count($op1) > count($op2)) {
return 1; // $op1 > $op2
}
foreach ($op1 as $key => $val) {
if (!array_key_exists($key, $op2)) {
return 1;
} elseif (static::lt($val, $op2[$key])) {
return -1;
} elseif (static::gt($val, $op2[$key])) {
return 1;
}
}
return 0; // $op1 == $op2
}
}
$a = [0 => ''];
$b = [0 => 0];
var_dump(Php8CompatUtils::eq($a, $b));
var_dump($a == $b);
preferences:
26.04 ms | 406 KiB | 5 Q