<?php
declare(strict_types=1);
$cases = [];
function add_case(array &$cases, string $name, mixed $expected, mixed $actual): void {
$cases[] = [
'name' => $name,
'passed' => ($actual === $expected),
'expected' => $expected,
'actual' => $actual,
];
}
// Case 1: GET param handler (your_php_file.php?param=value) with XSS-safe escaping
$handleGetParam = static function (array $query): string {
if (isset($query['param'])) {
return 'Received parameter: ' . htmlspecialchars((string) $query['param'], ENT_QUOTES, 'UTF-8');
}
return '';
};
add_case(
$cases,
'get_param_echo_handler',
[
'normal' => 'Received parameter: value',
'escaped' => 'Received parameter: <b>hi</b>',
'missing' => '',
],
[
'normal' => $handleGetParam(['param' => 'value']),
'escaped' => $handleGetParam(['param' => '<b>hi</b>']),
'missing' => $handleGetParam([]),
]
);
// 30-name array matching gethint.php
$names = [
'Anna', 'Brittany', 'Cinderella', 'Diana', 'Eva', 'Fiona', 'Gunda', 'Hege',
'Inga', 'Johanna', 'Kitty', 'Linda', 'Nina', 'Ophelia', 'Petunia', 'Amanda',
'Raquel', 'Cindy', 'Doris', 'Eve', 'Evita', 'Sunniva', 'Tove', 'Unni',
'Violet', 'Liza', 'Elizabeth', 'Ellen', 'Wenche', 'Vicky',
];
$getHint = static function (string $q) use ($names): string {
$hint = '';
if (strlen($q) > 0) {
$qLower = strtolower($q);
$len = strlen($q);
foreach ($names as $name) {
if ($qLower === strtolower(substr($name, 0, $len))) {
$hint = ($hint === '') ? $name : ($hint . ' , ' . $name);
}
}
}
return $hint === '' ? 'no suggestion' : $hint;
};
// Case 2: gethint.php prefix matching (case-insensitive)
add_case(
$cases,
'gethint_prefix_search_matches',
[
'q_a' => 'Anna , Amanda',
'q_ev' => 'Eva , Eve , Evita',
'q_An' => 'Anna',
],
[
'q_a' => $getHint('a'),
'q_ev' => $getHint('ev'),
'q_An' => $getHint('An'),
]
);
// Case 3: gethint.php empty & non-matching input
add_case(
$cases,
'gethint_empty_and_no_suggestion',
['q_zzz' => 'no suggestion', 'q_empty' => 'no suggestion'],
['q_zzz' => $getHint('zzz'), 'q_empty' => $getHint('')]
);
// Case 4: fetch() POST JSON handler (server.php)
$handleJsonPost = static function (string $rawBody): string {
$request = json_decode($rawBody, true);
if (is_array($request) && ($request['message'] ?? null) === 'Hello from client') {
$response = ['response' => 'Hello from PHP server!'];
} else {
$response = ['response' => 'Invalid message'];
}
return json_encode($response, JSON_UNESCAPED_UNICODE);
};
add_case(
$cases,
'fetch_post_json_server_handler',
[
'valid_json' => '{"response":"Hello from PHP server!"}',
'invalid_msg' => '{"response":"Invalid message"}',
'malformed_body' => '{"response":"Invalid message"}',
],
[
'valid_json' => $handleJsonPost('{"message":"Hello from client"}'),
'invalid_msg' => $handleJsonPost('{"message":"Other"}'),
'malformed_body' => $handleJsonPost('not-json'),
]
);
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-ajax-php-lab/explore-ajax-php.php
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-ajax-php.php,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、PHP 8.4 本地 CLI 环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1225.zip
课程关联工程包(登记关联95章) · 含当前章节或实例 · mbskycom202612a1232.zip