使用 yield 關鍵字實現簡單協程,記憶體高效處理大型資料集:
function readLargeFile($path) {
$handle = fopen($path, 'r');
while (($line = fgets($handle)) !== false) {
yield $line; // 暫停執行,返回值給呼叫者
}
fclose($handle);
}
foreach (readLargeFile('huge.log') as $line) {
// 處理每一行,記憶體常數級佔用
}
基於 Bcrypt,內建鹽值生成、成本因子、驗證,解決開發者自行實現加密易出錯問題:
// 雜湊 (自動生成隨機鹽、預設 cost=10)
$hash = password_hash($password, PASSWORD_DEFAULT);
// 驗證
if (password_verify($password, $hash)) {
// 密碼正確
}
// 需重新雜湊檢查 (如 cost 變更、演算法升級)
if (password_needs_rehash($hash, PASSWORD_DEFAULT, ['cost' => 12])) {
$hash = password_hash($password, PASSWORD_DEFAULT, ['cost' => 12]);
// 更新資料庫
}
支援演算法:PASSWORD_BCRYPT、PASSWORD_ARGON2I (7.2+)、PASSWORD_ARGON2ID (7.3+)。
finally 關鍵字完善異常處理流程,確保資源釋放:
try {
$db = new PDO($dsn, $user, $pass);
$db->beginTransaction();
// ... 業務邏輯
$db->commit();
} catch (Exception $e) {
$db->rollBack();
throw $e;
} finally {
$db = null; // 確保連線關閉
}
foreach 支援 list() 解構
$users = [
['John', 'Doe', 'john@example.com'],
['Jane', 'Smith', 'jane@example.com']
];
foreach ($users as list($first, $last, $email)) {
echo "$first $last <$email>\n";
}
array_column() 函數快速提取多維陣列單一欄位:
$records = [
['id' => 1, 'name' => 'Alice', 'age' => 25],
['id' => 2, 'name' => 'Bob', 'age' => 30],
['id' => 3, 'name' => 'Charlie', 'age' => 35]
];
$names = array_column($records, 'name'); // ['Alice', 'Bob', 'Charlie']
$ids = array_column($records, 'id', 'name'); // ['Alice'=>1, 'Bob'=>2, 'Charlie'=>3]
zend_extension=opcache.so、opcache.enable=1。| 變更項目 | 影響 | 遷移建議 |
|---|---|---|
| 移除 Windows XP / 2003 支援 | 無法在 XP/2003 運行 | 升級作業系統至 Windows 7/Server 2008 R2 以上 |
移除 ereg/eregi 等 POSIX 正則函數 | 呼叫報 Fatal Error | 改用 PCRE 函數 preg_match/preg_replace |
preg_replace /e 修飾符棄用 | E_DEPRECATED 警告 | 改用 preg_replace_callback |
| 類/介面/特徵名稱不區分大小寫 → 區分大小寫 | 自動載入失敗 | 統一類名大小寫、修正 spl_autoload_register 回調 |
| 常數名稱不再受 Locale 影響 | 非 ASCII 常數名行為變化 | 避免使用非 ASCII 字元命名常數 |
mysql_* 函數正式棄用 (E_DEPRECATED) | 大量警告 | 遷移至 PDO 或 MySQLi |