<?php
declare(strict_types=1);
$cases = [];
// Case 1: isset, empty, is_* predicates, gettype, get_debug_type
$valInt = 42;
$valStr = '3.14';
$valZeroStr = '0';
$valArr = [1, 2, 3];
$valObj = new stdClass();
$valNull = null;
$fn = static fn(int $x): int => $x + 1;
$typeActual = [
'isset_int' => isset($valInt),
'isset_null' => isset($valNull),
'empty_zero_str' => empty($valZeroStr),
'empty_nonempty_str' => empty($valStr),
'is_int' => is_int($valInt),
'is_string' => is_string($valStr),
'is_numeric_str' => is_numeric($valStr),
'is_array' => is_array($valArr),
'is_iterable_arr' => is_iterable($valArr),
'is_object' => is_object($valObj),
'is_scalar_int' => is_scalar($valInt),
'is_scalar_arr' => is_scalar($valArr),
'is_callable_fn' => is_callable($fn),
'is_null' => is_null($valNull),
'gettype_int' => gettype($valInt),
'gettype_float' => gettype(1.5),
'get_debug_type_int' => get_debug_type($valInt),
'get_debug_type_obj' => get_debug_type($valObj),
];
$typeExpected = [
'isset_int' => true,
'isset_null' => false,
'empty_zero_str' => true,
'empty_nonempty_str' => false,
'is_int' => true,
'is_string' => true,
'is_numeric_str' => true,
'is_array' => true,
'is_iterable_arr' => true,
'is_object' => true,
'is_scalar_int' => true,
'is_scalar_arr' => false,
'is_callable_fn' => true,
'is_null' => true,
'gettype_int' => 'integer',
'gettype_float' => 'double',
'get_debug_type_int' => 'int',
'get_debug_type_obj' => 'stdClass',
];
$cases[] = [
'name' => 'type_checks_and_inspectors',
'passed' => $typeActual === $typeExpected,
'expected' => $typeExpected,
'actual' => $typeActual,
];
// Case 2: intval, floatval, boolval, strval, settype, unset
$mutable = '128';
$setOk = settype($mutable, 'int');
$temp = 'gone';
unset($temp);
$convActual = [
'intval_prefix' => intval('42px'),
'intval_hex' => intval('ff', 16),
'floatval' => floatval('3.14rem'),
'boolval_zero' => boolval('0'),
'boolval_zero_float_str' => boolval('0.0'),
'strval' => strval(2026),
'settype_ok' => $setOk,
'settype_result' => $mutable,
'unset_checked' => isset($temp),
];
$convExpected = [
'intval_prefix' => 42,
'intval_hex' => 255,
'floatval' => 3.14,
'boolval_zero' => false,
'boolval_zero_float_str' => true,
'strval' => '2026',
'settype_ok' => true,
'settype_result' => 128,
'unset_checked' => false,
];
$cases[] = [
'name' => 'type_conversions_and_settype',
'passed' => $convActual === $convExpected,
'expected' => $convExpected,
'actual' => $convActual,
];
// Case 3: serialize / unserialize, var_export, print_r, get_resource_type
$payload = ['lang' => 'PHP', 'major' => 8, 'active' => true];
$ser = serialize($payload);
$unser = unserialize($ser, ['allowed_classes' => false]);
$exported = preg_replace('/\s+/', ' ', trim(var_export(['a' => 1, 'b' => true], true)));
$stream = fopen('php://memory', 'r+');
$resIsResource = is_resource($stream);
$resType = get_resource_type($stream);
fclose($stream);
$serActual = [
'serialized' => $ser,
'roundtrip' => $unser,
'var_export_compact' => $exported,
'is_resource' => $resIsResource,
'resource_type' => $resType,
];
$serExpected = [
'serialized' => 'a:3:{s:4:"lang";s:3:"PHP";s:5:"major";i:8;s:6:"active";b:1;}',
'roundtrip' => ['lang' => 'PHP', 'major' => 8, 'active' => true],
'var_export_compact' => "array ( 'a' => 1, 'b' => true, )",
'is_resource' => true,
'resource_type' => 'stream',
];
$cases[] = [
'name' => 'serialize_unserialize_and_export',
'passed' => $serActual === $serExpected,
'expected' => $serExpected,
'actual' => $serActual,
];
// Case 4: password_hash, password_verify, password_get_info, password_needs_rehash
$secret = 'BlueSky#2026';
$hash = password_hash($secret, PASSWORD_BCRYPT, ['cost' => 10]);
$info = password_get_info($hash);
$pwdActual = [
'verify_right' => password_verify($secret, $hash),
'verify_wrong' => password_verify('WrongPass', $hash),
'algo_name' => $info['algoName'],
'cost' => $info['options']['cost'],
'needs_rehash_same' => password_needs_rehash($hash, PASSWORD_BCRYPT, ['cost' => 10]),
'needs_rehash_higher' => password_needs_rehash($hash, PASSWORD_BCRYPT, ['cost' => 12]),
];
$pwdExpected = [
'verify_right' => true,
'verify_wrong' => false,
'algo_name' => 'bcrypt',
'cost' => 10,
'needs_rehash_same' => false,
'needs_rehash_higher' => true,
];
$cases[] = [
'name' => 'password_hash_and_verify',
'passed' => $pwdActual === $pwdExpected,
'expected' => $pwdExpected,
'actual' => $pwdActual,
];
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-variable-handling-functions-lab/explore-variable-handling-functions.php
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-variable-handling-functions.php,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、PHP 8.4 本地 CLI 环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联95章) · 含当前章节或实例 · mbskycom202612a1232.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1313.zip