这份 PHP 备忘单为快速查找最常用代码的正确语法提供了参考
入门
hello.php
1 | // 以 PHP 开放标签开头 |
PHP 运行命令
1 | php hello.php |
变量 Variables
1 | $boolean1 = true; |
查看: Types
字符串 Strings
1 | $url = "jaywcjlove.github.io"; |
查看: Strings
数组 Arrays
1 | $num = [1, 3, 5, 7, 9]; |
查看: Arrays
运算符 Operators
1 | $x = 1; |
查看: Operators
Include
vars.php
1 | // 以 PHP 开放标签开头。 |
test.php
1 |
|
功能 Functions
1 | function add($num1, $num2 = 1) { |
查看: Functions
注释 Comments
1 | # 这是一个单行 shell 样式的注释 |
常数 Constants
1 | const MY_CONST = "hello"; |
类 Classes
1 | class Student { |
查看: Classes
PHP 类型
布尔值 Boolean
1 | $boolean1 = true; |
布尔值不区分大小写
整数 Integer
1 | $int1 = 28; # => 28 |
另见: Integers
字符串 Strings
1 | echo 'this is a simple string'; |
查看: Strings
数组 Arrays
1 | $arr = array("hello", "world", "!"); |
查看: Arrays
浮点数 Float (Double)
1 | $float1 = 1.234; |
Null
1 | $a = null; |
可迭代对象 Iterables
1 | function bar(): iterable { |
PHP 字符串
字符串 String
1 | # => '$String' |
多行 Multi-line
1 | $str = "foo"; |
操作 Manipulation
1 | $s = "Hello Phper"; |
另见: 字符串函数
PHP 数组
定义
1 | $a1 = ["hello", "world", "!"] |
混合 int 和 string 键
1 | $array = array( |
短数组语法
1 | $array = [ |
多阵列
1 | $multiArray = [ |
多类型
1 | $array = array( |
操作
1 | $arr = array(5 => 1, 12 => 2); |
查看: 数组函数
索引迭代
1 | $array = array('a', 'b', 'c'); |
价值迭代
1 | $colors = array('red', 'blue', 'green'); |
关键迭代
1 | $arr = ["foo" => "bar", "bar" => "foo"]; |
串联阵列
1 | $a = [1, 2]; |
Into 函数
1 | $array = [1, 2]; |
Splat运算符
1 | function foo($first, ...$other) { |
PHP 运算符
算术
:- | - |
---|---|
+ |
添加 |
- |
减法 |
* |
乘法 |
/ |
分配 |
% |
取模 |
** |
求幂 |
分配
:- | - |
---|---|
a += b |
如同 a = a + b |
a -= b |
如同 a = a – b |
a *= b |
如同 a = a * b |
a /= b |
如同 a = a / b |
a %= b |
如同 a = a % b |
比较
:- | - |
---|---|
== |
平等的 |
=== |
完全相同的 |
!= |
不相等 |
<> |
不相等 |
!== |
不相同 |
< |
少于 |
> |
比…更棒 |
<= |
小于或等于 |
>= |
大于或等于 |
<=> |
小于/等于/大于 |
逻辑的
:- | - |
---|---|
and |
和 |
or |
或者 |
xor |
独家或 |
! |
不是 |
&& |
和 |
|| |
或者 |
算术
1 | // 算术 |
按位
:- | - |
---|---|
& |
和 |
` | ` |
^ |
异或(异或) |
~ |
不是 |
<< |
左移 |
>> |
右移 |
PHP 条件
If elseif else
1 | $a = 10; |
Switch
1 | $x = 0; |
三元运算符
1 | # => Does |
匹配
1 | $statusCode = 500; |
查看: Match
匹配表达式
1 | $age = 23; |
PHP 循环
while 循环
1 | $i = 1; |
do while 循环
1 | $i = 1; |
for i 循环
1 | # => 12345 |
break 跳出循环
1 | # => 123 |
continue 继续
1 | # => 1235 |
foreach 循环
1 | $a = ['foo' => 1, 'bar' => 2]; |
查看: Array iteration
PHP 函数
返回值
1 | function square($x) |
返回类型
1 | // 基本返回类型声明 |
可空返回类型
1 | // 在 PHP 7.1 中可用 |
查看: Nullable types
无效函数
1 | // 在 PHP 7.1 中可用 |
变量函数
1 | function bar($arg = '') |
匿名函数
1 | $greet = function($name) |
递归函数
1 | function recursion($x) |
默认参数
1 | function coffee($type = "cappuccino") |
箭头函数
1 | $y = 1; |
PHP 类
构造函数 Constructor
1 | class Student { |
继承 Inheritance
1 | class ExtendClass extends SimpleClass |
类变量 Classes variables
1 | class MyClass |
静态访问
1 | echo MyClass::MY_CONST; # => value |
魔术方法
1 | class MyClass |
接口
1 | interface Foo |
各种各样的
基本错误处理
1 | try { |
PHP 8.0 中的异常
1 | $nullableValue = null; |
自定义异常
1 | class MyException extends Exception { |
用法
1 | try { |
Nullsafe 运算符
1 | // 从 PHP 8.0.0 开始,这一行: |
另见: Nullsafe 运算符
常用表达
1 | $str = "Visit jaywcjlove.github.io"; |
查看: PHP中的正则表达式
fopen() 模式
:- | - |
---|---|
r |
读 |
r+ |
读写,前置 |
w |
写入,截断 |
w+ |
读写,截断 |
a |
写,追加 |
a+ |
读写,追加 |
运行时定义的常量
1 | define("CURRENT_DATE", date('Y-m-d')); |
另见
- PHP 官方中文文档 (php.net)
- Learn X in Y minutes (learnxinyminutes.com)
评论