PHP 简介

PHP 的历史、执行流程、以及它在 Web 里的位置。

Beginner
Key PointsPHP 的核心竞争力是「请求-执行-销毁」模型:没有内存泄漏,也不需考虑并发。

执行流程

Text
浏览器 ──HTTP──▶ Nginx
                  │  匹配 .php,交给 PHP-FPM
                  ▼
              PHP-FPM(常驻进程池)
                  │  取一个空闲 worker
                  ▼
              PHP 解释器
                  │  1. 读 .php 文件
                  │  2. 编译成 opcode(有 opcache 就复用)
                  │  3. 执行,输出 HTML
                  ▼
              返回给 Nginx ──▶ 浏览器

Key point: every request starts from scratch. Globals, class definitions and connections are all destroyed at the end. The upside is no cross-request data leaks and no thread-safety concerns; the cost is rebuilding context each time.

和别的语言比

AspectPHPNode.jsJava
执行模型请求-销毁常驻事件循环常驻 JVM
并发多进程(FPM)单线程异步多线程
内存泄漏几乎不可能需注意需注意
启动成本每请求重建一次一次
上手难度低中高

第一个页面

PHP
<?php
// index.php
$visits = (int) ($_COOKIE['visits'] ?? 0) + 1;
setcookie('visits', (string) $visits, time() + 86400);
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><title>示例</title></head>
<body>
  <h1>你好,这是第 <?= $visits ?> 次访问</h1>
  <p>服务器时间:<?= date('Y-m-d H:i:s') ?></p>
</body>
</html>
Output
你好,这是第 3 次访问
服务器时间:2026-10-31 11:20:44

<?= $x ?> is shorthand for <?php echo $x; ?> and is everywhere in templates. It works even with short_open_tag off — <?= has been always-on since PHP 5.4.

为什么还在用

  • 部署简单 —— 上传文件就能跑,不需要编译打包
  • 生态成熟 —— WordPress、Laravel、大量 CMS 和电商系统
  • 招人容易 —— 会 PHP 的开发者基数大
  • 共享主机友好 —— 几乎所有虚拟主机都支持
PHP's bottleneck is usually the database and I/O, not the language. With opcache, 8.x performance is close to compiled languages — do not decide based on the old "PHP is slow" impression.

Friends & Partners