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

验证 PHP 变量大小写、动态类型切换及 local/global/static/parameter 四种作用域

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

$cases = [];

// Case 1: Variable assignment, case sensitivity, and dynamic typing
$x = 5;
$y = 6;
$Y = 60;
$z = $x + $y;
$txt = "Hello world!";
$dynamic = "text";
$t1 = gettype($dynamic);
$dynamic = 5;
$t2 = gettype($dynamic);
$dynamic = 10.5;
$t3 = gettype($dynamic);

$basicActual = [
    'sum' => $z,
    'lower_y' => $y,
    'upper_Y' => $Y,
    'txt' => $txt,
    'type_transitions' => [$t1, $t2, $t3],
];
$basicExpected = [
    'sum' => 11,
    'lower_y' => 6,
    'upper_Y' => 60,
    'txt' => 'Hello world!',
    'type_transitions' => ['string', 'integer', 'double'],
];
$cases[] = [
    'name' => 'variable_creation_and_case_sensitivity',
    'passed' => $basicActual === $basicExpected,
    'expected' => $basicExpected,
    'actual' => $basicActual,
];

// Case 2: Local vs global scope isolation (PHP 8 emits E_WARNING on undefined local)
$outerX = 5;
$warnings = [];
set_error_handler(static function (int $severity, string $message) use (&$warnings): bool {
    $warnings[] = [$severity, $message];
    return true;
});

function scopeIsolationDemo(): array
{
    $innerY = 10;
    /** @noinspection PhpUndefinedVariableInspection */
    $seenOuter = $outerX;
    return ['outer_seen_in_fn' => $seenOuter, 'inner_y' => $innerY];
}

function sameLocalNameOtherFn(): int
{
    $innerY = 99;
    return $innerY;
}

$fnResult = scopeIsolationDemo();
$otherResult = sameLocalNameOtherFn();
/** @noinspection PhpUndefinedVariableInspection */
$outerSeesInner = $innerY;
restore_error_handler();

$scopeActual = [
    'outer_seen_in_fn' => $fnResult['outer_seen_in_fn'],
    'inner_y' => $fnResult['inner_y'],
    'other_fn_inner_y' => $otherResult,
    'outer_sees_inner' => $outerSeesInner,
    'warning_count' => count($warnings),
    'warning_level_is_e_warning' => ($warnings[0][0] ?? 0) === E_WARNING,
];
$scopeExpected = [
    'outer_seen_in_fn' => null,
    'inner_y' => 10,
    'other_fn_inner_y' => 99,
    'outer_sees_inner' => null,
    'warning_count' => 2,
    'warning_level_is_e_warning' => true,
];
$cases[] = [
    'name' => 'local_vs_global_isolation',
    'passed' => $scopeActual === $scopeExpected,
    'expected' => $scopeExpected,
    'actual' => $scopeActual,
];

// Case 3: global keyword and $GLOBALS superglobal array
$gx = 5;
$gy = 10;
function addViaGlobalKeyword(): void
{
    global $gx, $gy;
    $gy = $gx + $gy;
}
addViaGlobalKeyword();

$ga = 5;
$gb = 10;
function addViaGlobalsArray(): void
{
    $GLOBALS['gb'] = $GLOBALS['ga'] + $GLOBALS['gb'];
}
addViaGlobalsArray();

$globalActual = ['after_global_keyword' => $gy, 'after_GLOBALS_array' => $gb];
$globalExpected = ['after_global_keyword' => 15, 'after_GLOBALS_array' => 15];
$cases[] = [
    'name' => 'global_keyword_and_superglobal',
    'passed' => $globalActual === $globalExpected,
    'expected' => $globalExpected,
    'actual' => $globalActual,
];

// Case 4: static local variable retains value across invocations while staying local
function nextCounter(): int
{
    static $counter = 0;
    $current = $counter;
    $counter++;
    return $current;
}
$seq = [nextCounter(), nextCounter(), nextCounter()];
$staticActual = [
    'sequence' => $seq,
    'in_globals' => array_key_exists('counter', $GLOBALS),
];
$staticExpected = [
    'sequence' => [0, 1, 2],
    'in_globals' => false,
];
$cases[] = [
    'name' => 'static_variable_persistence',
    'passed' => $staticActual === $staticExpected,
    'expected' => $staticExpected,
    'actual' => $staticActual,
];

// Case 5: Parameter scope is local to the function and passed by value by default
function doubleShift(int $paramX): int
{
    $paramX += 10;
    return $paramX;
}
$callerVal = 5;
$paramActual = [
    'returned' => doubleShift($callerVal),
    'caller_unchanged' => $callerVal,
];
$paramExpected = [
    'returned' => 15,
    'caller_unchanged' => 5,
];
$cases[] = [
    'name' => 'parameter_scope_passing',
    'passed' => $paramActual === $paramExpected,
    'expected' => $paramExpected,
    'actual' => $paramActual,
];

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

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

下载课程关联工程包:PHP

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

下载课程关联工程包:PHP

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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