@ 2025-01-03T04:03:24Z <?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);
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 8.4.3 0.009 0.010 18.86 8.4.2 0.042 0.007 17.43 8.4.1 0.037 0.009 17.46 8.3.16 0.014 0.007 16.42 8.3.15 0.041 0.005 16.53 8.3.14 0.040 0.003 16.62 8.3.13 0.036 0.006 16.47 8.3.12 0.037 0.005 16.50 8.3.11 0.021 0.014 16.54 8.3.10 0.036 0.005 16.39 8.3.9 0.037 0.009 16.67 8.3.8 0.037 0.007 16.52 8.3.7 0.032 0.008 16.52 8.3.6 0.032 0.008 16.61 8.3.5 0.028 0.005 16.53 8.3.4 0.022 0.007 17.63 8.3.3 0.026 0.011 17.46 8.3.2 0.026 0.010 17.44 8.3.1 0.024 0.011 17.56 8.3.0 0.022 0.006 17.51 8.2.27 0.031 0.005 16.25 8.2.26 0.043 0.006 16.49 8.2.25 0.029 0.013 16.73 8.2.24 0.022 0.017 16.41 8.2.23 0.027 0.012 16.33 8.2.22 0.025 0.009 16.51 8.2.21 0.025 0.011 16.29 8.2.20 0.031 0.006 16.64 8.2.19 0.033 0.005 16.34 8.2.18 0.024 0.015 16.51 8.2.17 0.026 0.010 17.73 8.2.16 0.025 0.006 17.70 8.2.15 0.030 0.010 17.53 8.2.14 0.036 0.005 17.40 8.2.13 0.034 0.006 17.53 8.2.12 0.031 0.011 17.63 8.2.11 0.035 0.008 17.53 8.2.10 0.030 0.008 17.56 8.2.9 0.028 0.011 17.69 8.2.8 0.032 0.008 17.27 8.2.7 0.032 0.006 17.36 8.2.6 0.019 0.011 17.58 8.2.5 0.037 0.003 17.49 8.2.4 0.031 0.005 17.60 8.2.3 0.031 0.008 17.53 8.2.2 0.025 0.011 17.38 8.2.1 0.025 0.010 17.65 8.2.0 0.028 0.007 17.57
preferences:dark mode live preview
28.03 ms | 403 KiB | 5 Q