$ipLong = ip2long('192.168.1.1'); // -1062731775 (有符號) 或 sprintf('%u', ip2long(...))
$ipStr = long2ip($ipLong);
// 資料庫: INT UNSIGNED (4 bytes) vs CHAR(15) (15 bytes)
// 範圍查詢: WHERE ip BETWEEN ip2long('192.168.1.0') AND ip2long('192.168.1.255')
if (checkdnsrr(array_pop(explode('@', $email)), 'MX')) {
// 域名有郵件伺服器
}
// PHP 7+ $name = $user['name'] ?? 'Guest'; // null 合併 $role = $user['is_admin'] ? 'admin' : 'user'; // 三元 $page = ($page ?? 1) > 0 ? $page : 1; // 組合
勿重複造輪子:composer require league/csv monolog/monolog phpmailer/phpmailer。檢查 packagist.org 尋找成熟套件。
// 除錯/教學用
highlight_file(__FILE__); // 當前檔案
highlight_string('<?php echo "hi"; ?>'); // 字串
// 入口檔第一行
error_reporting(0);
ini_set('display_errors', '0');
ini_set('log_errors', '1');
ini_set('error_log', '/var/log/php/error.log');
$compressed = gzcompress($longText, 6); // 1-9 壓縮等級 $original = gzuncompress($compressed); // 典型文本壓縮率 90%+,適用 Blob/LongText 欄位
function process(string $input, string &$clean, int &$count): bool {
$clean = trim($input);
$count = strlen($clean);
return $count > 0;
}
$clean = '';
$count = 0;
if (process(' hello ', $clean, $count)) {
echo "$clean ($count)"; // hello (5)
}
PDO::ATTR_EMULATE_PREPARES => false 強制原生預處理。htmlspecialchars、JS 用 json_encode、Shell 用 escapeshellarg。public function __construct(public string $name) {}$result = match($status) { 1 => 'ok', 2 => 'fail', default => 'unknown' };func(name: 'test', id: 1)function foo(int|string $id): bool|array {}