PHP 教程

PHP 是什么、它现在的样子、以及这套教程怎么学。

Beginner
Key PointsPHP 是专为 Web 设计的服务端脚本语言,请求进来执行、生成 HTML 返回。

PHP 是什么

PHP (PHP: Hypertext Preprocessor) is a server-side scripting language from 1995. Its execution model is direct: the web server receives a request, hands it to the PHP interpreter, which outputs HTML back to the browser. No long-running process, no elaborate bootstrap.

PHP
<?php
$name = "世界";
echo "你好,{$name}!\n";
echo "当前 PHP 版本:" . PHP_VERSION . "\n";
Output
你好,世界!
当前 PHP 版本:8.3.12

它现在的样子

PHP is no longer just a templating language. Version 8.x brings JIT, type declarations, enums, readonly properties, match and constructor promotion, and with Composer's ecosystem (Laravel, Symfony) it supports serious engineering.

VersionYearKey features
5.62014常量表达式、可变参数
7.02015性能翻倍、标量类型声明、??
7.42019箭头函数、类型化属性
8.02020JIT、命名参数、match、构造器提升
8.12021枚举、只读属性、never
8.32023类型化类常量、json_validate

能做什么

  • 网站与 API —— WordPress、Laravel、Symfony
  • 内容管理 —— 全球 40% 以上的网站跑在 PHP 上
  • 命令行脚本 —— 用 php script.php 直接跑
  • 不适合 —— 长驻内存服务、CPU 密集计算(每次请求重建上下文的模型拖后腿)

学习路线

  1. 语法 —— 变量、类型、字符串、数组、流程控制
  2. 函数与数组 —— PHP 的数组函数极其丰富,值得单独花时间
  3. 面向对象 —— 类、接口、trait、命名空间、自动加载
  4. Web —— 表单、Cookie、Session、文件上传、数据库
  5. 工程 —— Composer、PDO、框架
Examples are verified on PHP 8.3. Check with php -v; anything below 8.0 cannot run enums, match or named arguments.

Friends & Partners