PHP Misc

Miscellaneous: binary packing, runtime info, connection state, constant checks.

Intermediate
Key PointsNo single theme, but these come up constantly when diagnosing environments.

Binary packing

PHP
<?php
$bin = pack("Nn", 0x01020304, 0x0506);
echo strlen($bin), "\n";
echo bin2hex($bin), "\n";

print_r(unpack("Nfirst/nsecond", $bin));

// 常见格式
echo bin2hex(pack("C", 255)), "\n";
echo bin2hex(pack("v", 0x0102)), "\n";
echo bin2hex(pack("V", 0x01020304)), "\n";
Output
6
010203040506
Array
(
    [first] => 16909060
    [second] => 1286
)
ff
0201
04030201
CodeMeaningBytes
C无符号字符1
n16 位大端2
v16 位小端2
N32 位大端4
V32 位小端4
J64 位大端8
a定长字符串(补 \0)n
A定长字符串(补空格)n

n/N are network byte order (big-endian); v/V are little-endian. Protocol headers and binary files rely on these.

Runtime info

PHP
<?php
echo php_sapi_name(), "\n";
echo PHP_OS_FAMILY, "\n";
echo PHP_INT_SIZE, "\n";
echo PHP_EOL === "\n" ? "LF" : "CRLF", "\n";

echo defined("PHP_VERSION") ? "有 PHP_VERSION" : "无", "\n";
echo function_exists("str_contains") ? "有 str_contains" : "无", "\n";
echo extension_loaded("json") ? "json 已加载" : "未加载", "\n";
echo get_extension_funcs("json") !== false ? "可列出 json 函数" : "不可列出", "\n";
Output
cli
Windows
8
LF
有 PHP_VERSION
有 str_contains
json 已加载
可列出 json 函数
PHP_EOL is \r\n on Windows and \n on Linux. Using it makes output platform-dependent — write "\n" when determinism matters.

Connection and timing

PHP
<?php
echo connection_status(), "\n";
echo connection_aborted() ? "已中断" : "未中断", "\n";
echo ignore_user_abort() ? "忽略中断" : "响应中断", "\n";

$start = microtime(true);
usleep(10000);
$ms = round((microtime(true) - $start) * 1000, 0);
echo $ms >= 9 ? "usleep 生效" : "usleep 异常", "\n";

set_time_limit(5);
echo ini_get("max_execution_time") . "\n";
Output
0
未中断
响应中断
usleep 生效
5

connection_status() returns 0 normal, 1 aborted, 2 timed out, 3 both. Long tasks must poll it so they stop when the user leaves.

PHP
<?php
ignore_user_abort(false);

for ($i = 1; $i <= 3; $i++) {
    if (connection_aborted()) {
        exit("客户端已断开\n");
    }
    echo "处理第 $i 批\n";
    flush();
    usleep(1000);
}
echo "全部完成\n";
Output
处理第 1 批
处理第 2 批
处理第 3 批
全部完成
ignore_user_abort(false) alone does not stop the script — PHP only notices on the next output. You must flush() and check connection_aborted() inside the loop.

Other common helpers

PHP
<?php
// 常量检查
define("APP_ENV", "dev");
echo constant("APP_ENV"), "\n";

// 唯一 ID(非密码学安全,仅用于日志串联)
$id = uniqid("req_", true);
echo str_starts_with($id, "req_") ? "前缀正确" : "前缀错误", "\n";

// 内存
echo memory_get_usage() > 0 ? "内存可读" : "不可读", "\n";

// 高亮源码(调试用)
echo str_contains(highlight_string("<?php \$a = 1;", true), "highlight") ? "可高亮" : "不可高亮", "\n";
Output
dev
前缀正确
内存可读
可高亮
uniqid() is microsecond-based, predictable and collidable — never use it for tokens or reset links. Use random_bytes / random_int.
FunctionUseCaution
pack / unpack二进制编解码注意字节序
php_sapi_name运行环境(cli / fpm-fcgi)分支逻辑要覆盖全部 SAPI
connection_aborted客户端是否断开要配合 flush()
set_time_limit脚本最长执行时间CLI 下默认不限
memory_get_usage当前内存占用排查内存泄漏常用
uniqid唯一 ID不安全

Friends & Partners