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

运行 PHP 8.4 基础测验 21 项核心知识点自检脚本

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

$cases = [];

// Case 1: core syntax, file extension, output constructs, string concat/len, constants
define('QUIZ_SITE', 'mbsky');
const QUIZ_VER = 84;
ob_start();
echo 'A';
$printRet = print 'B';
$capturedOut = ob_get_clean();

$c1Actual = [
    'ext_is_php' => str_ends_with(__FILE__, '.php'),
    'captured_echo_print' => $capturedOut,
    'print_returns_1' => ($printRet === 1),
    'write_not_builtin_fn' => !function_exists('write'),
    'concat_and_strlen' => strlen('Hello' . ' ' . 'PHP'),
    'define_and_const' => QUIZ_SITE . ':' . QUIZ_VER,
];
$c1Expected = [
    'ext_is_php' => true,
    'captured_echo_print' => 'AB',
    'print_returns_1' => true,
    'write_not_builtin_fn' => true,
    'concat_and_strlen' => 9,
    'define_and_const' => 'mbsky:84',
];
$cases[] = [
    'name' => 'quiz_syntax_and_variables',
    'passed' => $c1Actual === $c1Expected,
    'expected' => $c1Expected,
    'actual' => $c1Actual,
];

// Case 2: isset() vs empty() vs is_null() truth table
$valNull = null;
$valEmptyStr = '';
$valZero = 0;
$valZeroStr = '0';
$valOk = 'ok';

$c2Actual = [
    'unset_isset' => isset($notDefinedVar),
    'unset_empty' => empty($notDefinedVar),
    'null_checks' => [isset($valNull), empty($valNull), is_null($valNull)],
    'empty_str_checks' => [isset($valEmptyStr), empty($valEmptyStr), is_null($valEmptyStr)],
    'zero_int_checks' => [isset($valZero), empty($valZero), is_null($valZero)],
    'zero_str_checks' => [isset($valZeroStr), empty($valZeroStr), is_null($valZeroStr)],
    'ok_str_checks' => [isset($valOk), empty($valOk), is_null($valOk)],
];
$c2Expected = [
    'unset_isset' => false,
    'unset_empty' => true,
    'null_checks' => [false, true, true],
    'empty_str_checks' => [true, true, false],
    'zero_int_checks' => [true, true, false],
    'zero_str_checks' => [true, true, false],
    'ok_str_checks' => [true, false, false],
];
$cases[] = [
    'name' => 'quiz_state_checks_isset_empty_isnull',
    'passed' => $c2Actual === $c2Expected,
    'expected' => $c2Expected,
    'actual' => $c2Actual,
];

// Case 3: arrays (count, foreach) and superglobals ($_GET, $_POST, $_REQUEST, $_SERVER['PHP_SELF'])
$langs = ['PHP', 'SQL', 'HTML'];
$collected = [];
foreach ($langs as $idx => $lang) {
    $collected[] = "{$idx}:{$lang}";
}
$_GET['topic'] = 'quiz';
$_POST['score'] = '21';
$_REQUEST['topic'] = 'quiz';

$c3Actual = [
    'count_langs' => count($langs),
    'foreach_items' => $collected,
    'superglobals_present' => isset($_GET['topic'], $_POST['score'], $_REQUEST['topic'], $_SERVER['PHP_SELF']),
    'php_self_name' => basename((string)$_SERVER['PHP_SELF']),
];
$c3Expected = [
    'count_langs' => 3,
    'foreach_items' => ['0:PHP', '1:SQL', '2:HTML'],
    'superglobals_present' => true,
    'php_self_name' => 'explore-quiz.php',
];
$cases[] = [
    'name' => 'quiz_arrays_loops_and_superglobals',
    'passed' => $c3Actual === $c3Expected,
    'expected' => $c3Expected,
    'actual' => $c3Actual,
];

// Case 4: file_exists(), time(), date(), header(), include/require/exit language tokens
date_default_timezone_set('UTC');
$tokens = token_get_all('<?php include "a.php"; require "b.php"; include_once "c.php"; exit(0);');
$tokenNames = [];
foreach ($tokens as $tok) {
    if (is_array($tok)) {
        $name = token_name($tok[0]);
        if (in_array($name, ['T_INCLUDE', 'T_REQUIRE', 'T_INCLUDE_ONCE', 'T_EXIT'], true)) {
            $tokenNames[] = $name;
        }
    }
}

$c4Actual = [
    'file_exists_self' => file_exists(__FILE__),
    'time_is_int_gt_epoch' => is_int(time()) && time() > 1700000000,
    'date_epoch_utc' => date('Y-m-d H:i:s', 0),
    'header_fn_exists' => function_exists('header'),
    'control_tokens' => $tokenNames,
];
$c4Expected = [
    'file_exists_self' => true,
    'time_is_int_gt_epoch' => true,
    'date_epoch_utc' => '1970-01-01 00:00:00',
    'header_fn_exists' => true,
    'control_tokens' => ['T_INCLUDE', 'T_REQUIRE', 'T_INCLUDE_ONCE', 'T_EXIT'],
];
$cases[] = [
    'name' => 'quiz_files_time_and_control',
    'passed' => $c4Actual === $c4Expected,
    'expected' => $c4Expected,
    'actual' => $c4Actual,
];

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

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

下载课程关联工程包:PHP

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

下载课程关联工程包:PHP

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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