蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

实测 PHP 8.4 全部运算符类别、?? 与 <=> 及优先级与括号分组行为

PHPWindows x64、PHP 8.4 本地 CLI 环境。explore-operators.php
explore-operators.php203 行
<?php
declare(strict_types=1);

$cases = [];

// Case 1: Arithmetic, unary negation, bitwise NOT (~), string concat (.), and intdiv()
$x = 10;
$y = 6;
$c1Actual = [
    'add' => $x + $y,
    'sub' => $x - $y,
    'mul' => $x * $y,
    'div_round6' => round($x / $y, 6),
    'mod' => $x % $y,
    'neg' => -$x,
    'bitnot_0' => ~0,
    'bitnot_1' => ~1,
    'bitnot_2' => ~2,
    'concat' => 'Hi' . 'Ha',
    'intdiv_10_3' => intdiv(10, 3),
    'intdiv_neg7_2' => intdiv(-7, 2),
];
$c1Expected = [
    'add' => 16,
    'sub' => 4,
    'mul' => 60,
    'div_round6' => 1.666667,
    'mod' => 4,
    'neg' => -10,
    'bitnot_0' => -1,
    'bitnot_1' => -2,
    'bitnot_2' => -3,
    'concat' => 'HiHa',
    'intdiv_10_3' => 3,
    'intdiv_neg7_2' => -3,
];
$cases[] = [
    'name' => 'arithmetic_bitwise_and_intdiv',
    'passed' => $c1Actual === $c1Expected,
    'expected' => $c1Expected,
    'actual' => $c1Actual,
];

// Case 2: Assignment operators (=, +=, -=, *=, /=, %=, .=) and pre/post increment & decrement
$a = 10;
$b = 20; $b += 100;
$c = 50; $c -= 25;
$d = 5;  $d *= 6;
$e = 10; $e /= 5;
$f = 15; $f %= 4;
$s = 'Hello'; $s .= ' world!';

$ix = 10; $preInc = ++$ix;
$iy = 10; $postInc = $iy++;
$iz = 5;  $preDec = --$iz;
$ii = 5;  $postDec = $ii--;

$c2Actual = [
    'assign_vals' => [$a, $b, $c, $d, $e, $f, $s],
    'pre_inc' => [$preInc, $ix],
    'post_inc' => [$postInc, $iy],
    'pre_dec' => [$preDec, $iz],
    'post_dec' => [$postDec, $ii],
];
$c2Expected = [
    'assign_vals' => [10, 120, 25, 30, 2, 3, 'Hello world!'],
    'pre_inc' => [11, 11],
    'post_inc' => [10, 11],
    'pre_dec' => [4, 4],
    'post_dec' => [5, 4],
];
$cases[] = [
    'name' => 'assignment_string_and_incdec',
    'passed' => $c2Actual === $c2Expected,
    'expected' => $c2Expected,
    'actual' => $c2Actual,
];

// Case 3: Comparison, Logical (and/or/xor/&&/||/!), and Array (+, ==, ===, !=, <>, !==) operators
$arr1 = ['a' => 'red', 'b' => 'green'];
$arr2 = ['b' => 'blue', 'c' => 'yellow'];
$arrSameOrderDiff = ['b' => 'green', 'a' => 'red'];
$lx = 6;
$ly = 3;

$c3Actual = [
    'cmp_loose' => (100 == '100'),
    'cmp_strict' => (100 === '100'),
    'cmp_neq_bang' => (5 != 8),
    'cmp_neq_angle' => (5 <> 8),
    'cmp_not_ident' => (5 !== '5'),
    'cmp_gt_lt_ge_le' => [50 > 90, 50 < 90, 5 >= 8, 5 <= 8],
    'logic' => [
        ($lx < 10 and $ly > 1),
        ($lx == 6 or $ly == 5),
        ($lx == 6 xor $ly == 3),
        ($lx < 10 && $ly > 1),
        ($lx == 5 || $ly == 5),
        !($lx == $ly),
    ],
    'array_union_left_wins' => ($arr1 + $arr2),
    'array_eq_diff_order' => ($arr1 == $arrSameOrderDiff),
    'array_ident_diff_order' => ($arr1 === $arrSameOrderDiff),
    'array_neq_ops' => [($arr1 != $arr2), ($arr1 <> $arr2), ($arr1 !== $arrSameOrderDiff)],
];
$c3Expected = [
    'cmp_loose' => true,
    'cmp_strict' => false,
    'cmp_neq_bang' => true,
    'cmp_neq_angle' => true,
    'cmp_not_ident' => true,
    'cmp_gt_lt_ge_le' => [false, true, false, true],
    'logic' => [true, true, false, true, false, true],
    'array_union_left_wins' => ['a' => 'red', 'b' => 'green', 'c' => 'yellow'],
    'array_eq_diff_order' => true,
    'array_ident_diff_order' => false,
    'array_neq_ops' => [true, true, true],
];
$cases[] = [
    'name' => 'comparison_logical_and_array_ops',
    'passed' => $c3Actual === $c3Expected,
    'expected' => $c3Expected,
    'actual' => $c3Actual,
];

// Case 4: Ternary (?:), Elvis, Null Coalescing (??), PHP_EOL, and Spaceship (<=>)
$query = ['empty_user' => ''];
$warnings = 0;
set_error_handler(static function () use (&$warnings): bool {
    $warnings++;
    return true;
});
$ternaryUser = isset($query['user']) ? $query['user'] : 'nobody';
$elvisUser = $query['empty_user'] ?: 'nobody';
$coalesceMissing = $query['user'] ?? 'nobody';
$coalesceEmpty = $query['empty_user'] ?? 'nobody';
restore_error_handler();

$c4Actual = [
    'ternary_missing' => $ternaryUser,
    'elvis_empty_string' => $elvisUser,
    'coalesce_missing' => $coalesceMissing,
    'coalesce_keeps_empty_str' => $coalesceEmpty,
    'zero_warnings' => $warnings === 0,
    'eol_non_empty' => PHP_EOL !== '',
    'spaceship_int' => [1 <=> 1, 1 <=> 2, 2 <=> 1],
    'spaceship_float' => [1.5 <=> 1.5, 1.5 <=> 2.5, 2.5 <=> 1.5],
    'spaceship_str' => ['a' <=> 'a', 'a' <=> 'b', 'b' <=> 'a'],
];
$c4Expected = [
    'ternary_missing' => 'nobody',
    'elvis_empty_string' => 'nobody',
    'coalesce_missing' => 'nobody',
    'coalesce_keeps_empty_str' => '',
    'zero_warnings' => true,
    'eol_non_empty' => true,
    'spaceship_int' => [0, -1, 1],
    'spaceship_float' => [0, -1, 1],
    'spaceship_str' => [0, -1, 1],
];
$cases[] = [
    'name' => 'ternary_null_coalesce_and_spaceship',
    'passed' => $c4Actual === $c4Expected,
    'expected' => $c4Expected,
    'actual' => $c4Actual,
];

// Case 5: Operator precedence (|| vs or with =, parentheses, and PHP 8 + before .)
$pa = 3;
$pb = false;
$pc = $pa or $pb;
$pd = $pa || $pb;
$noParen = 1 + 2 * 3;
$withParen = (1 + 2) * 3;
$php8AddBeforeConcat = "sum=" . 1 + 2;

$c5Actual = [
    'assign_or_val' => $pc,
    'assign_or_type' => gettype($pc),
    'assign_pipe_val' => $pd,
    'assign_pipe_type' => gettype($pd),
    'no_paren' => $noParen,
    'with_paren' => $withParen,
    'php8_add_before_concat' => $php8AddBeforeConcat,
];
$c5Expected = [
    'assign_or_val' => 3,
    'assign_or_type' => 'integer',
    'assign_pipe_val' => true,
    'assign_pipe_type' => 'boolean',
    'no_paren' => 7,
    'with_paren' => 9,
    'php8_add_before_concat' => 'sum=3',
];
$cases[] = [
    'name' => 'precedence_and_parentheses',
    'passed' => $c5Actual === $c5Expected,
    'expected' => $c5Expected,
    'actual' => $c5Actual,
];

echo json_encode(['version' => PHP_VERSION, 'cases' => $cases], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n";
本例需在 Windows x64、PHP 8.4 本地 CLI 环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、PHP 8.4 本地 CLI 环境。

入口文件:projects/php-operators-lab/explore-operators.php

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-operators.php,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、PHP 8.4 本地 CLI 环境。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:PHP

课程关联工程包(登记关联95章) · 含当前章节或实例 · mbskycom202612a1232.zip

下载课程关联工程包:PHP

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1278.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习