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

在 PHP 8.4 CLI 验证杂项常量、二进制封包、高精度计时与源码压缩

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

$cases = [];

// Case 1: define / defined / constant & connection status constants
define('MBSKY_APP_ENV', 'local');
$constActual = [
    'defined_existing' => defined('MBSKY_APP_ENV'),
    'defined_missing' => defined('MBSKY_NON_EXISTENT'),
    'constant_value' => constant('MBSKY_APP_ENV'),
    'connection_constants' => [
        'normal' => CONNECTION_NORMAL,
        'aborted' => CONNECTION_ABORTED,
        'timeout' => CONNECTION_TIMEOUT,
    ],
    'connection_status' => connection_status(),
    'connection_aborted' => connection_aborted(),
];
$constExpected = [
    'defined_existing' => true,
    'defined_missing' => false,
    'constant_value' => 'local',
    'connection_constants' => [
        'normal' => 0,
        'aborted' => 1,
        'timeout' => 2,
    ],
    'connection_status' => 0,
    'connection_aborted' => 0,
];
$cases[] = [
    'name' => 'constants_define_and_lookup',
    'passed' => $constActual === $constExpected,
    'expected' => $constExpected,
    'actual' => $constActual,
];

// Case 2: pack / unpack binary framing
$packed = pack('nC2', 0x1234, 0xAB, 0xCD);
$unpacked = unpack('nheader/Cflag_a/Cflag_b', $packed);
$packActual = [
    'byte_length' => strlen($packed),
    'hex' => bin2hex($packed),
    'unpacked' => $unpacked,
];
$packExpected = [
    'byte_length' => 4,
    'hex' => '1234abcd',
    'unpacked' => ['header' => 4660, 'flag_a' => 171, 'flag_b' => 205],
];
$cases[] = [
    'name' => 'binary_pack_and_unpack',
    'passed' => $packActual === $packExpected,
    'expected' => $packExpected,
    'actual' => $packActual,
];

// Case 3: uniqid / hrtime / usleep timing
$id1 = uniqid('req_', true);
$t1 = hrtime(true);
usleep(1000);
$t2 = hrtime(true);
$id2 = uniqid('req_', true);
$timeActual = [
    'prefix_ok' => str_starts_with($id1, 'req_') && str_starts_with($id2, 'req_'),
    'length_ok' => strlen($id1) === 27 && strlen($id2) === 27,
    'distinct_ids' => $id1 !== $id2,
    'hrtime_is_int' => is_int($t1) && is_int($t2),
    'elapsed_positive' => ($t2 - $t1) > 0,
];
$timeExpected = [
    'prefix_ok' => true,
    'length_ok' => true,
    'distinct_ids' => true,
    'hrtime_is_int' => true,
    'elapsed_positive' => true,
];
$cases[] = [
    'name' => 'uniqid_and_hrtime_monotonic',
    'passed' => $timeActual === $timeExpected,
    'expected' => $timeExpected,
    'actual' => $timeActual,
];

// Case 4: highlight_string / php_strip_whitespace / ini_get defaults
$highlighted = highlight_string("<?php\n\$x = 42;\n", true);
$tmpFile = tempnam(sys_get_temp_dir(), 'mbsky_misc_');
file_put_contents($tmpFile, "<?php\n// comment line\n\$a   =   1;\n/* block */\necho \$a;\n");
$stripped = trim(php_strip_whitespace($tmpFile));
@unlink($tmpFile);
$codeActual = [
    'highlight_has_pre_or_code' => str_contains($highlighted, '<code') && str_contains($highlighted, '42'),
    'stripped_code' => $stripped,
    'ignore_user_abort_default' => ignore_user_abort(),
];
$codeExpected = [
    'highlight_has_pre_or_code' => true,
    'stripped_code' => "<?php\n \$a = 1; echo \$a;",
    'ignore_user_abort_default' => 0,
];
$cases[] = [
    'name' => 'highlight_and_strip_whitespace',
    'passed' => $codeActual === $codeExpected,
    'expected' => $codeExpected,
    'actual' => $codeActual,
];

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-ref-misc-lab/explore-ref-misc.php

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

下载课程关联工程包:PHP

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

下载课程关联工程包:PHP

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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