PHP 字符串
介绍 PHP 字符串变量声明、并置运算符 .、strlen() 字节长度与 mb_strlen() 字符长度差异、strpos() 0 起始索引陷阱以及 PHP 8 字符串判断函数。
学完这一章,你能够
- 介绍 PHP 字符串变量声明、并置运算符 .、strlen() 字节长度与 mb_strlen() 字符长度差异、strpos() 0 起始索引陷阱以及 PHP 8 字符串判断函数。
字符串变量与并置运算符(.)
字符串变量用于保存与处理文本数据,创建后可直接传入函数或继续拼接处理。为变量赋予文本时必须使用单引号或双引号包裹;本站 PHP 8.4.27 实测 `$txt = "Hello world!";` 与单引号 `'Hello world!'` 严格相等。
PHP 中用于连接两个字符串的专用运算符是并置运算符 `.`(点号)。若需在两个字符串之间插入空格,可连续使用两次并置运算。本站实测 `"Hello world!" . " " . "What a nice day!"` 精确输出 `Hello world! What a nice day!`。
strlen() 长度计算与多字节中文差异
`strlen()` 用于返回字符串的**字节数**,常用于边界检查或循环终止条件。对纯 ASCII 文本 `"Hello world!"`,本站实测 `strlen()` 返回 `12`;但在 UTF-8 编码下每个中文汉字占 3 字节,实测 `strlen('你好PHP')` 返回 `9` 字节,若需按可见字符个数统计应使用 `mbstring` 扩展的 `mb_strlen('你好PHP', 'UTF-8')`,实测返回 `5`。
strpos() 定位、PHP 8 现代函数与参考手册
`strpos()` 用于查找子串在目标字符串中首次出现的位置,位置索引从 `0` 开始计数;若未找到则返回布尔值 `false`。本站实测 `strpos('Hello world!', 'world')` 返回 `6`,`strpos('Hello world!', 'Hello')` 返回 `0`,查找 `'PHP'` 返回 `false`。由于 `0 == false` 在松散比较中为真,判断“是否找到”必须使用严格比较 `!== false`。
除基础查找外,PHP 8.0 起新增了语义更直观的 `str_contains()`、`str_starts_with()` 与 `str_ends_with()`:本站实测三者对 `'Hello world!'` 检测 `'world'`、`'Hello'`、`'!'` 均返回 `true`,检测 `'PHP'` 返回 `false`。更多字符串处理函数与示例将在本教程 String 参考手册章节系统展开。
字符串字面量引号规则提示
| 提示主题 | 核心说明 |
|---|---|
| 文本值必须加引号 | 将文本赋给变量时必须用单引号或双引号包裹;双引号会解析变量与转义字符,单引号保持字面内容。 |
实测 PHP 字符串并置、strlen 与 mb_strlen 对比、strpos 零索引陷阱及 PHP 8 辅助函数
<?php
declare(strict_types=1);
$cases = [];
// Case 1: String variables, single vs double quotes, and concatenation operator (.)
$txt = "Hello world!";
$txtSingle = 'Hello world!';
$txt1 = "Hello world!";
$txt2 = "What a nice day!";
$joined = $txt1 . " " . $txt2;
$c1Actual = [
'txt' => $txt,
'quotes_equal' => $txt === $txtSingle,
'joined' => $joined,
];
$c1Expected = [
'txt' => 'Hello world!',
'quotes_equal' => true,
'joined' => 'Hello world! What a nice day!',
];
$cases[] = [
'name' => 'string_var_and_concatenation',
'passed' => $c1Actual === $c1Expected,
'expected' => $c1Expected,
'actual' => $c1Actual,
];
// Case 2: strlen() returns UTF-8 byte length vs mb_strlen() returning character count
$c2Actual = [
'ascii_strlen' => strlen('Hello world!'),
'utf8_strlen_bytes' => strlen('你好PHP'),
'utf8_mb_strlen_chars' => mb_strlen('你好PHP', 'UTF-8'),
];
$c2Expected = [
'ascii_strlen' => 12,
'utf8_strlen_bytes' => 9,
'utf8_mb_strlen_chars' => 5,
];
$cases[] = [
'name' => 'strlen_bytes_vs_mb_strlen_chars',
'passed' => $c2Actual === $c2Expected,
'expected' => $c2Expected,
'actual' => $c2Actual,
];
// Case 3: strpos() 0-based index, index-0 strict check (!== false), and not-found false
$posWorld = strpos('Hello world!', 'world');
$posHello = strpos('Hello world!', 'Hello');
$posMissing = strpos('Hello world!', 'PHP');
$c3Actual = [
'world_pos' => $posWorld,
'hello_pos' => $posHello,
'hello_found_strict' => $posHello !== false,
'missing_is_false' => $posMissing === false,
];
$c3Expected = [
'world_pos' => 6,
'hello_pos' => 0,
'hello_found_strict' => true,
'missing_is_false' => true,
];
$cases[] = [
'name' => 'strpos_zero_index_and_not_found',
'passed' => $c3Actual === $c3Expected,
'expected' => $c3Expected,
'actual' => $c3Actual,
];
// Case 4: Modern PHP 8.0+ string inspection helpers (str_contains, str_starts_with, str_ends_with)
$c4Actual = [
'contains_world' => str_contains('Hello world!', 'world'),
'contains_php' => str_contains('Hello world!', 'PHP'),
'starts_hello' => str_starts_with('Hello world!', 'Hello'),
'ends_bang' => str_ends_with('Hello world!', '!'),
];
$c4Expected = [
'contains_world' => true,
'contains_php' => false,
'starts_hello' => true,
'ends_bang' => true,
];
$cases[] = [
'name' => 'php8_str_contains_helpers',
'passed' => $c4Actual === $c4Expected,
'expected' => $c4Expected,
'actual' => $c4Actual,
];
echo json_encode(['version' => PHP_VERSION, 'cases' => $cases], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n";
- 确认字符串拼接、UTF-8 下 strlen('你好PHP')=9 vs mb_strlen=5、strpos 索引 6/0/false 与 str_contains 等 PHP 8 函数全通过。
所需环境:Windows x64、PHP 8.4 本地 CLI 环境。
当前源码 SHA256:88b61440ebd7c40a6c9d4b88b552c5f645283dfef0bdf053e0701dcef62f5856
本地运行
解压后按 README 说明在对应本地环境运行脚本。
所需环境:Windows x64、PHP 8.4 本地 CLI 环境。
ZIP SHA256:b641a202cc73159c59f1da32eac55408edf07c9ec93aa9778ad77f82ceb4b666
常见错误与边界
- 实际端口、文档资源与应用配置都应和测试条件一致。
练习与解答
为什么用 `if (strpos($s, 'Hello'))` 判断字符串是否包含 `'Hello'` 会出现漏判?应如何正确编写?
展开参考解答
当 `'Hello'` 位于字符串开头(下标 `0`)时,`strpos()` 返回整型 `0`,在 `if` 布尔转换中 `0` 会被当成 `false` 导致误判未找到;正确写法是 `strpos($s, 'Hello') !== false` 或在 PHP 8+ 直接使用 `str_contains($s, 'Hello')`。