1. Installing PHP 8.4
PHP 8.4 is the latest stable version and supports all major operating systems. Choose the installation method for your platform.
macOS (Homebrew)
On macOS, Homebrew is recommended — one command does it all:
# 安装 Homebrew(如果尚未安装)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# 安装 PHP 8.4
brew install php
# 确认版本
php -v
Tip: Homebrew installs the latest stable version by default. If an older version of PHP is already on your system, Homebrew will automatically set the new version as default.
Ubuntu / Debian
The default Ubuntu repositories may not include the latest version. It's recommended to add the ppa:ondrej/php repository:
# 添加 PHP PPA 源
sudo add-apt-repository ppa:ondrej/php
sudo apt update
# 安装 PHP 8.4 及常用扩展
sudo apt install php8.4 php8.4-cli php8.4-common php8.4-curl php8.4-mbstring php8.4-xml php8.4-zip
# 确认版本
php -v
Windows
There are several installation options on Windows:
# 方式一:使用 Scoop(推荐)
scoop install php
# 方式二:使用 Chocolatey
choco install php
# 方式三:手动下载
# 从 https://windows.php.net/download/ 下载 VS17 x64 Non Thread Safe 版本
# 解压到 C:\php,将 C:\php 添加到系统 PATH 环境变量
Verify Installation
Regardless of platform, you should see output similar to the following after installation:
$ php -v
PHP 8.4.0 (cli) (built: Nov 21 2024 10:30:05) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.4.0, Copyright (c) Zend Technologies
with Zend OPcache v8.4.0, Copyright (c), by Zend Technologies
🔄 Comparison with Other Languages
Python uses python3 --version, Node.js uses node -v, Java uses java --version. PHP's version check command is php -v — consistent style.
2. php.ini Configuration
php.ini is PHP's core configuration file that controls various runtime behaviors. First, locate it:
# 查看加载的配置文件
php --ini
# 输出示例:
# Configuration File (php.ini) Path: /etc/php/8.4/cli
# Loaded Configuration File: /etc/php/8.4/cli/php.ini
# Scan for additional .ini files in: /etc/php/8.4/cli/conf.d
You can also use php -i | grep "php.ini" for a quick lookup. Below are the most commonly adjusted settings for development:
| Config | Recommended Value (Dev) | Description |
|---|---|---|
| display_errors | On |
Display errors on the page (must be disabled in production) |
| error_reporting | E_ALL |
Report all error levels, recommended during development |
| date.timezone | Asia/Shanghai |
Default timezone to avoid date function warnings |
| memory_limit | 256M |
Max memory per script; Composer operations may need more |
| upload_max_filesize | 20M |
Maximum allowed upload file size |
| post_max_size | 25M |
Max POST request data size; should be larger than upload_max_filesize |
| max_execution_time | 30 |
Max script execution time (seconds); 0 means no limit |
Example: Setting development environment configuration in php.ini:
; 开发环境推荐配置
display_errors = On
error_reporting = E_ALL
date.timezone = Asia/Shanghai
memory_limit = 256M
upload_max_filesize = 20M
post_max_size = 25M
max_execution_time = 30
Note: After modifying php.ini, you need to restart PHP or the web server for changes to take effect. In CLI mode, the configuration is re-read on every execution.
3. Built-in Development Server
PHP comes with a lightweight built-in development server — no need to install Apache or Nginx to quickly test your code. It's ideal for development and learning.
First, create a simple index.php file:
<?php
echo "Hello, PHP 8.4!";
Then start the development server in the same directory:
# 启动内置开发服务器
php -S localhost:8000
# 指定文档根目录
php -S localhost:8000 -t public/
# 输出示例:
# [Tue Mar 31 10:00:00 2026] PHP 8.4.0 Development Server started
# Listening on http://localhost:8000
# Press Ctrl-C to quit.
Open http://localhost:8000 in your browser to see the output. You can also use a more complete test page:
<?php
// info.php - 查看完整的 PHP 配置信息
phpinfo();
⚠️ Development Only: The built-in server is single-threaded and does not support concurrent requests — it cannot be used in production. For production, use Nginx + PHP-FPM or Apache + mod_php.
🔄 Comparison with Python's Built-in Server
| PHP | php -S localhost:8000 — parses and executes .php files |
| Python | python -m http.server 8000 — serves static files only |
PHP's built-in server is more capable — it can handle dynamic requests directly without an additional framework.
4. Installing Composer
Composer is PHP's dependency management tool, equivalent to npm for Node.js, pip for Python, and Maven for Java. Nearly all modern PHP projects depend on it.
Installing Composer
# macOS / Linux
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
# 或通过 Homebrew(macOS)
brew install composer
# Windows(使用 Scoop)
scoop install composer
# 验证安装
composer --version
Initialize a Project
# 在项目目录中初始化
mkdir my-php-project && cd my-php-project
composer init
This will guide you through creating composer.json, which is the project's dependency configuration file:
{
"name": "yourname/my-php-project",
"description": "我的 PHP 项目",
"type": "project",
"require": {
"php": ">=8.4"
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
}
}
Install Dependencies
# 安装一个包(例如 HTTP 客户端 Guzzle)
composer require guzzlehttp/guzzle
# 安装开发依赖(例如 PHPUnit 测试框架)
composer require --dev phpunit/phpunit
# 安装 composer.json 中定义的所有依赖
composer install
Using Autoloading
Composer provides an autoloading mechanism — just include one line to use all installed packages:
<?php
// 引入 Composer 自动加载文件(所有项目的入口都需要这一行)
require __DIR__ . '/vendor/autoload.php';
// 现在可以直接使用任何已安装的包
use GuzzleHttp\Client;
$client = new Client();
$response = $client->get('https://api.github.com');
echo $response->getBody();
🔄 Package Manager Comparison
| Language | Tool | Config File | Lock File |
| PHP | Composer | composer.json | composer.lock |
| Node.js | npm | package.json | package-lock.json |
| Python | pip | requirements.txt | — |
| Java | Maven | pom.xml | — |
Tip: The vendor/ directory contains all downloaded dependencies and should be added to .gitignore. For team collaboration, only commit composer.json and composer.lock — others can run composer install to restore dependencies.
5. Hello World
Let's write a slightly more complete PHP script to understand the basic syntax elements:
<?php
// hello.php - 第一个 PHP 脚本
// 变量以 $ 开头,无需声明类型
$name = "PHP";
$version = 8.4;
$features = ["类型声明", "枚举", "纤程", "属性钩子"];
// echo 输出内容,双引号支持变量插值
echo "Hello, {$name} {$version}!\n";
// 字符串拼接用 .(不是 +)
echo "当前版本:" . $name . " " . $version . "\n";
// 遍历数组
echo "PHP 8.x 新特性:\n";
foreach ($features as $index => $feature) {
echo " " . ($index + 1) . ". {$feature}\n";
}
// 简单函数
function greet(string $who): string {
return "欢迎学习 {$who}!";
}
echo greet("PHP 8.4") . "\n";
Save the file and run it from the command line:
$ php hello.php
Hello, PHP 8.4!
当前版本:PHP 8.4
PHP 8.x 新特性:
1. 类型声明
2. 枚举
3. 纤程
4. 属性钩子
欢迎学习 PHP 8.4!
Quick Syntax Reference:
- • PHP code must start with
<?php - • Variables start with
$and are case-sensitive - • Every statement ends with
; - • String concatenation uses
., not+ - • Double quotes support variable interpolation
"Hello, {$name}", single quotes do not
🔄 Execution Method Comparison
| PHP | php hello.php |
| Python | python hello.py |
| Node.js | node hello.js |
| Java | javac Hello.java && java Hello |
PHP, like Python, is an interpreted language — no compilation needed, just run directly.
6. VS Code Configuration
VS Code is one of the most popular editors for PHP development. Install the following extensions for a near-IDE development experience:
PHP Intelephense
High-performance PHP language server providing intelligent completion, type inference, go-to-definition, refactoring, and more.
bmewburn.vscode-intelephense-client
PHP Debug
Xdebug debug adapter supporting breakpoint debugging, variable inspection, and call stack tracing.
xdebug.php-debug
PHP CS Fixer
Code formatting tool that automatically fixes code style, maintaining consistent coding standards across the team.
junstyle.php-cs-fixer
Quick install in VS Code: Press Ctrl+Shift+X (macOS: Cmd+Shift+X) to open the Extensions panel, then search for the extension name to install.
It's recommended to add a .vscode/settings.json to your project:
{
"php.validate.executablePath": "/usr/local/bin/php",
"editor.formatOnSave": true,
"[php]": {
"editor.defaultFormatter": "junstyle.php-cs-fixer"
},
"intelephense.environment.phpVersion": "8.4.0"
}
Debug Setup: Before using the PHP Debug extension, you need to install the Xdebug extension. Install it via pecl install xdebug, then add zend_extension=xdebug to php.ini to enable it.
7. Chapter Summary
🛠️ Installing PHP
macOS: brew install php, Ubuntu: apt install php8.4, Windows: use Scoop or manual download. Verify with php -v.
⚙️ Configuring php.ini
Use php --ini to find the config file. Enable display_errors and E_ALL error reporting in development, and set the correct timezone.
🖥️ Built-in Server
php -S localhost:8000 quickly starts a development server, suitable for learning and debugging, not for production.
📦 Composer
PHP's package manager. composer init to create a project, composer require to install dependencies, require 'vendor/autoload.php' to enable autoloading.
📝 Basic Syntax
Starts with <?php, variables use $ prefix, statements end with ;, string concatenation uses ., double quotes support variable interpolation.
💻 Development Tools
VS Code + Intelephense for intelligent completion, PHP Debug + Xdebug for breakpoint debugging, PHP CS Fixer for unified code style.