php

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

PHP

PHP

A popular specific-purpose scripting language that is especially suited to web development.
一种广受欢迎的专用脚本语言,尤其适用于Web开发。

When to Use

适用场景

  • Server-side web development
  • WordPress / CMS development
  • Content-heavy sites
  • Rapid prototyping
  • 服务器端Web开发
  • WordPress / CMS开发
  • 内容密集型网站
  • 快速原型开发

Quick Start

快速开始

php
<?php
$name = "World";
echo "Hello, $name!";

$colors = ["red", "green", "blue"];
foreach ($colors as $color) {
    echo $color . "<br>";
}
?>
php
<?php
$name = "World";
echo "Hello, $name!";

$colors = ["red", "green", "blue"];
foreach ($colors as $color) {
    echo $color . "<br>";
}
?>

Core Concepts

核心概念

Arrays

数组

PHP arrays are versatile (serve as lists, maps, stacks, etc.).
php
$user = [
    "id" => 1,
    "name" => "Foo"
];
PHP数组用途广泛(可作为列表、映射、栈等使用)。
php
$user = [
    "id" => 1,
    "name" => "Foo"
];

Superglobals

超全局变量

Built-in variables available in all scopes (
$_GET
,
$_POST
,
$_SESSION
).
在所有作用域中都可用的内置变量(
$_GET
$_POST
$_SESSION
)。

OOP

面向对象编程(OOP)

PHP 8+ has robust object-oriented features including properties, attributes, and match expressions.
PHP 8+具备强大的面向对象特性,包括属性、attributes和匹配表达式。

Best Practices

最佳实践

Do:
  • Use Composer for dependency management
  • Type hint function arguments and return types
  • Use PDO for database access (prepared statements)
  • Follow PSR standards (PSR-12)
Don't:
  • Use
    mysql_
    functions (deprecated/removed)
  • Trust user input (sanitize/validate)
  • leave
    display_errors
    on in production
建议
  • 使用Composer进行依赖管理
  • 为函数参数和返回值添加类型提示
  • 使用PDO进行数据库访问(预处理语句)
  • 遵循PSR标准(PSR-12)
不建议
  • 使用
    mysql_
    系列函数(已废弃/移除)
  • 信任用户输入(需进行过滤/验证)
  • 在生产环境中开启
    display_errors

References

参考资料