Skip to content

Latest commit

History

History
972 lines (791 loc) · 14.8 KB

File metadata and controls

972 lines (791 loc) · 14.8 KB

PHP 备忘清单

这份 PHP 备忘单为快速查找最常用代码的正确语法提供了参考

入门

hello.php

<?php// 以 PHP 开放标签开头echo"Hello World\n";
print("Hello jaywcjlove.github.io");
?>

PHP 运行命令

$ php hello.php

变量 Variables

$boolean1 = true;
$boolean2 = True;
$int = 12;
$float = 3.1415926;
unset($float); // 删除变量$str1 = "How are you?";
$str2 = 'Fine, thanks';

查看: Types

字符串 Strings

$url = "jaywcjlove.github.io";
echo"I'm learning PHP at $url";
// 连接字符串echo"I'm learning PHP at " . $url;
$hello = "Hello, ";
$hello .= "World!";
echo$hello; # => Hello, World!

查看: Strings

数组 Arrays

$num = [1, 3, 5, 7, 9];
$num[5] = 11;
unset($num[2]); // 删除变量print_r($num); # => 1 3 7 9 11echocount($num); # => 5

查看: Arrays

运算符 Operators

$x = 1;
$y = 2;
$sum = $x + $y;
echo$sum; # => 3

查看: Operators

Include

vars.php

<?php// 以 PHP 开放标签开头。$fruit = 'apple';
echo"I was imported";
return'Anything you like.';
?>

test.php

<?phpinclude'vars.php';
echo$fruit . "\n"; # => apple/* 与 include 相同,如果不能包含则导致错误*/require'vars.php';
// 也有效include('vars.php');
require('vars.php');
// 通过 HTTP 包含include'http://x.com/file.php';
// 包含和返回语句$result = include'vars.php';
echo$result; # => Anything you like.?>

功能 Functions

functionadd($num1, $num2 = 1) {
return$num1 + $num2;
}
echoadd(10); # => 11echoadd(10, 5); # => 15

查看: Functions

注释 Comments

# 这是一个单行 shell 样式的注释// 这是一行 c++ 风格的注释/* 这是一个多行注释 另一行注释 */

常数 Constants

constMY_CONST = "hello";
echoMY_CONST; # => hello# => MY_CONST is: helloecho'MY_CONST is: ' . MY_CONST; 

类 Classes

class Student {
publicfunction__construct($name) {
$this->name = $name;
}
}
$alex = newStudent("Alex");

查看: Classes

PHP 类型

布尔值 Boolean

$boolean1 = true;
$boolean2 = TRUE;
$boolean3 = false;
$boolean4 = FALSE;
$boolean5 = (boolean) 1; # => true$boolean6 = (boolean) 0; # => false

布尔值不区分大小写

整数 Integer

$int1 = 28; # => 28$int2 = -32; # => -32$int3 = 012; # => 10 (octal)$int4 = 0x0F; # => 15 (hex)$int5 = 0b101; # => 5 (binary)# => 2000100000 (decimal, PHP 7.4.0)$int6 = 2_000_100_000;

另见: Integers

字符串 Strings

echo'this is a simple string';

查看: Strings

数组 Arrays

$arr = array("hello", "world", "!");

查看: Arrays

浮点数 Float (Double)

$float1 = 1.234;
$float2 = 1.2e7;
$float3 = 7E-10;
$float4 = 1_234.567; // as of PHP 7.4.0var_dump($float4); // float(1234.567)$float5 = 1 + "10.5"; # => 11.5$float6 = 1 + "-1.3e3"; # => -1299

Null

$a = null;
$b = 'Hello php!';
echo$a ?? 'a is unset'; # => a is unsetecho$b ?? 'b is unset'; # => Hello php$a = array();
$a == null # => true$a === null # => falseis_null($a) # => false

可迭代对象 Iterables

functionbar(): iterable {
return [1, 2, 3];
}
functiongen(): iterable {
yield1;
yield2;
yield3;
}
foreach (bar() as$value) {
echo$value; # => 123
} 

PHP 字符串

字符串 String

# => '$String'$sgl_quotes = '$String';
# => 'This is a $String.'$dbl_quotes = "This is a $sgl_quotes.";
# => a tab character.$escaped = "a \t tab character.";
# => a slash and a t: \t$unescaped = 'a slash and a t: \t';

多行 Multi-line

$str = "foo";
// 未插值的多行$nowdoc = <<<'END'Multi line string$str
END;
// 将执行字符串插值$heredoc = <<<ENDMulti line$strEND;

操作 Manipulation

$s = "Hello Phper";
echostrlen($s); # => 11echosubstr($s, 0, 3); # => Helechosubstr($s, 1); # => ello Phperechosubstr($s, -4, 3);# => hpeechostrtoupper($s); # => HELLO PHPERechostrtolower($s); # => hello phperechostrpos($s, "l"); # => 2var_dump(strpos($s, "L")); # => false

另见: 字符串函数

PHP 数组

定义

$a1 = ["hello", "world", "!"]
$a2 = array("hello", "world", "!");
$a3 = explode(",", "apple,pear,peach");

混合 int 和 string 键

$array = array(
"foo" => "bar",
"bar" => "foo",
100 => -100,
-100 => 100,
);
var_dump($array);

短数组语法

$array = [
"foo" => "bar",
"bar" => "foo",
];

多阵列

$multiArray = [ [1, 2, 3],
[4, 5, 6],
[7, 8, 9],
];
print_r($multiArray[0][0]) # => 1print_r($multiArray[0][1]) # => 2print_r($multiArray[0][2]) # => 3

多类型

$array = array(
"foo" => "bar",
42 => 24,
"multi" => array(
"dim" => array(
"a" => "foo"
)
)
);
# => string(3) "bar"var_dump($array["foo"]);
# => int(24)var_dump($array[42]); # => string(3) "foo"var_dump($array["multi"]["dim"]["a"]);

操作

$arr = array(5 => 1, 12 => 2);
$arr[] = 56; // 附加$arr["x"] = 42; // 用键添加sort($arr); // 排序
unset($arr[5]); // 消除
unset($arr); // 移除所有

查看: 数组函数

索引迭代

$array = array('a', 'b', 'c');
$count = count($array);
for ($i = 0; $i < $count; $i++) {
echo"i:{$i}, v:{$array[$i]}\n";
}

价值迭代

$colors = array('red', 'blue', 'green');
foreach ($colorsas$color) {
echo"Do you like $color?\n";
}

关键迭代

$arr = ["foo" => "bar", "bar" => "foo"];
foreach ( $arras$key => $value )
{
echo"key: " . $key . "\n";
echo"val: {$arr[$key]}\n";
}

串联阵列

$a = [1, 2];
$b = [3, 4];
// PHP 7.4 以后# => [1, 2, 3, 4]$result = [...$a, ...$b];

Into 函数

$array = [1, 2];
functionfoo(int$a, int$b) {
echo$a; # => 1echo$b; # => 2
}
foo(...$array);

Splat运算符

functionfoo($first, ...$other) {
var_dump($first); # => avar_dump($other); # => ['b', 'c']
}
foo('a', 'b', 'c'/*, ...*/ );
// 或functionfoo($first, string ...$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独家或
!不是
&&
||或者

算术

// 算术$sum = 1 + 1; // 2$difference = 2 - 1; // 1$product = 2 * 2; // 4$quotient = 2 / 1; // 2// 速记算术$num = 0;
$num += 1; // 将 $num 增加 1echo$num++; // 打印 1(评估后的增量)echo ++$num; // 打印 3(评估前的增量)$num /= $float; // 将商除并分配给 $num

按位

:--
&
``
^异或(异或)
~不是
<<左移
>>右移

PHP 条件

If elseif else

$a = 10;
$b = 20;
if ($a > $b) {
echo"a is bigger than b";
} elseif ($a == $b) {
echo"a is equal to b";
} else {
echo"a is smaller than b";
}

Switch

$x = 0;
switch ($x) {
case'0':
print"it's zero";
break; case'two':
case'three':
// do somethingbreak;
default:
// do something
}

三元运算符

# => Doesprint (false ? 'Not' : 'Does');
$x = false;
# => Doesprint($x ?: 'Does');
$a = null;
$b = 'Does print';
# => a is unsertecho$a ?? 'a is unset';
# => printecho$b ?? 'b is unset';

匹配

$statusCode = 500;
$message = match($statusCode) {
200, 300 => null,
400 => '未找到',
500 => '服务器错误',
default => '已知状态码',
};
echo$message; # => 服务器错误

查看: Match

匹配表达式

$age = 23;
$result = match (true) {
$age >= 65 => 'senior',
$age >= 25 => 'adult',
$age >= 18 => 'young adult',
default => 'kid',
};
echo$result; # => young adult

PHP 循环

while 循环

$i = 1;
# => 12345while ($i <= 5) {
echo$i++;
}

do while 循环

$i = 1;
# => 12345do {
echo$i++;
} while ($i <= 5);

for i 循环

# => 12345for ($i = 1; $i <= 5; $i++) {
echo$i;
}

break 跳出循环

# => 123for ($i = 1; $i <= 5; $i++) {
if ($i === 4) {
break;
}
echo$i;
}

continue 继续

# => 1235for ($i = 1; $i <= 5; $i++) {
if ($i === 4) {
continue;
}
echo$i;
}

foreach 循环

$a = ['foo' => 1, 'bar' => 2];
# => 12foreach ($aas$k) {
echo$k;
}

查看: Array iteration

PHP 函数

返回值

functionsquare($x)
{
return$x * $x;
}
echosquare(4); # => 16

返回类型

// 基本返回类型声明functionsum($a, $b): float {/*...*/}
functionget_item(): string {/*...*/}
class C {}
// 返回一个对象functiongetC(): C { returnnewC; }

可空返回类型

// 在 PHP 7.1 中可用functionnullOrString(int$v) : ?string
{
return$v % 2 ? "odd" : null;
}
echonullOrString(3); # => oddvar_dump(nullOrString(4)); # => NULL

查看: Nullable types

无效函数

// 在 PHP 7.1 中可用functionvoidFunction(): void
{
echo'Hello';
return;
}
voidFunction(); # => Hello

变量函数

functionbar($arg = '')
{
echo"In bar(); arg: '$arg'.\n";
}
$func = 'bar';
$func('test'); # => In bar(); arg: test

匿名函数

$greet = function($name)
{
printf("Hello %s\r\n", $name);
};
$greet('World'); # => Hello World$greet('PHP'); # => Hello PHP

递归函数

functionrecursion($x)
{
if ($x < 5) {
echo"$x";
recursion($x + 1);
}
}
recursion(1); # => 1234

默认参数

functioncoffee($type = "cappuccino")
{
return"Making a cup of $type.\n";
}
# => 制作一杯卡布奇诺echocoffee();
# => 制作一杯echocoffee(null);
# => 制作一杯浓缩咖啡echocoffee("espresso");

箭头函数

$y = 1;
$fn1 = fn($x) => $x + $y;
// 相当于按值使用 $y:$fn2 = function ($x) use ($y) {
return$x + $y;
};
echo$fn1(5); # => 6echo$fn2(5); # => 6

PHP 类

构造函数 Constructor

class Student {
publicfunction__construct($name) {
$this->name = $name;
}
publicfunctionprint() {
echo"Name: " . $this->name;
}
}
$alex = newStudent("Alex");
$alex->print(); # => Name: Alex

继承 Inheritance

class ExtendClass extends SimpleClass
{
// 重新定义父方法functiondisplayVar()
{
echo"Extending class\n";
parent::displayVar();
}
}
$extended = newExtendClass();
$extended->displayVar();

类变量 Classes variables

class MyClass
{
constMY_CONST = 'value';
static$staticVar = 'static';
// 可见度publicstatic$var1 = 'pubs';
// 仅限类privatestatic$var2 = 'pris';
// 类和子类protectedstatic$var3 = 'pros';
// 类和子类protected$var6 = 'pro';
// 仅限类private$var7 = 'pri'; }

静态访问

echo MyClass::MY_CONST; # => valueecho MyClass::$staticVar; # => static

魔术方法

class MyClass
{
// 对象被视为字符串publicfunction__toString()
{
return$property;
}
// 与 __construct() 相反publicfunction__destruct()
{
print"Destroying";
}
}

接口

interface Foo {
publicfunctiondoSomething();
}
interface Bar
{
publicfunctiondoSomethingElse();
}
class Cls implements Foo, Bar {
publicfunctiondoSomething() {}
publicfunctiondoSomethingElse() {}
}

各种各样的

基本错误处理

try {
// 做一点事
} catch (Exception$e) {
// 处理异常
} finally {
echo"Always print!";
}

PHP 8.0 中的异常

$nullableValue = null;
try {
$value = $nullableValue ?? thrownewInvalidArgumentException();
} catch (InvalidArgumentException) { // 变量是可选的// 处理我的异常echo"print me!";
}

自定义异常

class MyException extends Exception {
// 做一点事
}

用法

try {
$condition = true;
if ($condition) {
thrownewMyException('bala');
}
} catch (MyException$e) {
// 处理我的异常
}

Nullsafe 运算符

// 从 PHP 8.0.0 开始,这一行:$result = $repo?->getUser(5)?->name;
// 相当于下面的代码:if (is_null($repo)) {
$result = null;
} else {
$user = $repository->getUser(5);
if (is_null($user)) {
$result = null;
} else {
$result = $user->name;
}
}

另见: Nullsafe 运算符

常用表达

$str = "Visit jaywcjlove.github.io";
echopreg_match("/qu/i", $str); # => 1

查看: PHP中的正则表达式

fopen() 模式

:--
r
r+读写,前置
w写入,截断
w+读写,截断
a写,追加
a+读写,追加

运行时定义的常量

define("CURRENT_DATE", date('Y-m-d'));
// 一种可能的表示echoCURRENT_DATE; # => 2021-01-05# => CURRENT_DATE is: 2021-01-05echo'CURRENT_DATE is: ' . CURRENT_DATE; 

另见