蓝色天空网mbsky.com · 知识 · 实践 · 创造PHP · 本地阅读预览
教程 / 后端开发 / PHP

PHP 测验

梳理 PHP 基础测验覆盖的 21 项核心语法与函数考点,用 PHP 8.4 本地断言脚本逐项实测核验,并修正参考题库中的技术细节偏差。

本站独立整理的内容草稿。实例显示的是预期结果;浏览器、服务端运行与技术内容验收尚待完成,本页只用于阅读和图文核对。

学完这一章,你能够

PHP 基础知识挑战范围与嵌入说明

本章节聚焦 PHP 基础阶段知识自测,覆盖文件扩展名、起止标签、变量与输出、表单与超级全局变量、空值检测函数、字符串与数组操作、文件包含、常量、日期时间及流程终止共 21 项基础考点。原始参考页面依赖外部测验脚本与页面内联题库驱动前端交互,本站遵循安全规范绝不执行来源脚本,而是将全部知识点改写为离线可运行的 PHP 8.4 实测断言集。

核心考点 PHP 8.4 离线实测核验

语法、输出与状态检测核验:本站 PHP 8.4.27 实测验证脚本以 `.php` 结尾、以 `<?php` 起始、变量以 `$` 开头、语句以 `;` 结尾、字符串连接使用 `.` 且 `strlen('Hello PHP') === 9`、`define()` 与 `const` 均可定义常量。需特别订正一项常见误区:PHP 内置输出语句为 `echo` 与 `print`(实测 `print` 返回 `1`),但并不存在内置的 `write` 语句(实测 `function_exists('write') === false`),学习时切勿混淆。针对高频易错的状态判断三函数,实测真值表:`isset()` 仅对“已声明且非 `null`”为真(对 `""`、`0`、`"0"` 均为 `true`);`empty()` 对未定义、`null`、`""`、`0`、`"0"` 均为 `true`;`is_null()` 仅对 `null` 为 `true`。

数组、超级全局变量与常用函数核验:实测 `count()` 返回数组长度 `3`,`foreach` 顺序遍历键值,`$_GET`、`$_POST`、`$_REQUEST` 收集表单与查询参数且 `$_SERVER['PHP_SELF']` 给出当前脚本名;文件与时间函数中 `file_exists(__FILE__)` 返回 `true`,`time()` 返回当前整型 Unix 时间戳,`date('Y-m-d H:i:s', 0)` 在 UTC 时区格式化为 `'1970-01-01 00:00:00'`,`header()` 可用于发送 HTTP 重定向头;词法分析器 `token_get_all()` 确认 `include`、`require`、`include_once`(文件包含)与 `exit()`(终止执行)均为 PHP 关键字结构。完成基础自测后,可继续进入表单、文件、会话与数据库进阶章节巩固练习。

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

php · 阅读源码
<?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";
预期结果 · 待实际运行核验
  • 核对语法/输出/常量(含证实 write 非内建函数)、isset/empty/is_null 完整真值表、数组与超级全局变量,以及 file_exists/time/date/header/include/require/exit 校验全部通过。
运行 PHP 8.4 基础测验 21 项核心知识点自检脚本

所需环境:Windows x64、PHP 8.4 本地 CLI 环境。

当前源码 SHA256:e795dbc2753ad1663d383f2ec027b843d4e403b35b57fa7bfe3e5632d37bd5e2

下载本站示例源码

本地运行

解压后按 README 说明在对应本地环境运行脚本。

下载PHP 测验实验

所需环境:Windows x64、PHP 8.4 本地 CLI 环境。

ZIP SHA256:42d0ee44696ae54055b0ce38ba9d65d8539c2c4c5f41f227b43a014586d06b38

常见错误与边界

  • 实际端口、文档资源与应用配置都应和测试条件一致。

练习与解答

当 $x = '0' 时,isset($x)、empty($x) 与 is_null($x) 分别返回什么布尔值?echo、print 与 write 哪些是 PHP 内置输出方式?

展开参考解答

对 $x = '0',isset($x) 为 true(已定义且非 null),empty($x) 为 true(字符串 '0' 在 PHP 中视为空值),is_null($x) 为 false。PHP 内置输出语言结构只有 echo 与 print,并不存在内置的 write 语句。

规范与官方依据