1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
| <?php
// 注释
/*
* 多行注释
*/
$variable = 5; # 弱类型语言
echo "xxx: $variable"; # 打印变量直接写
echo PHP_EOL; # 换行符
echo "str" . $variable . "str2";
echo "a",1,2,3;
print "a123" # almost the same, but only take one param
var_dump($variable); # return data type and value
# Boolean
true;
false;
# Array
$arr = array(1,2,3);
$arrlength = count($arr);
$arr = array("1"=>1); # associative array
$arr['1'];
foreach($arr as $x => $x_val){
//
}
array(
array(1,2),
array(2,3)
);
sort($arr); # ascend
rsort($arr); # descend
asort($arr); # ascend, val
ksort($arr); # ascend, key
arsort($arr); # descend, val
krsort($arr); # descend, key
# Null
null;
# string func
strlen("a"); # 1
str_word_count("a b"); # 2, 空格分隔,词数
strrev("ab"); # ba, reverse
strpos("a b", "a"); # 0
strpos("a b", "c"); # false
str_replace("b", "c", "a b"); # a c
is_numeric("123");
# number func
PHP_INT_MAX, PHP_INT_MIN, PHP_INT_SIZE;
is_int($variable); # below are aliases
is_integer($variable);
is_long($variable);
(int)$variable; # cast
PHP_FLOAT_MAX, PHP_FLOAT_MIN, PHP_FLOAT_DIG, PHP_FLOAT_EPSILON;
is_float($variable);
is_double($variable); # alias
is_finite($variable);
is_infinite($variable);
is_nan($variable);
pi()
min(list);
max(list);
abs(-1);
sqrt(4);
$a ** $b;
round(0.4);
rand();
rand(10, 100);
$a === $b; # check value and type
$a <> $b; # !=
$a++;
$a += $b;
$a and $b;
$a or $b;
$a xor $b;
$x = expr1 ?? expr2; # 根据expr1是否为null判断
if(condition){
//
}
elseif(condition){
//
}
else{
//
}
switch($n) {
case label1:
//
break;
default:
break;
}
while(condition){
//
}
do{
//
}while(condition);
for(;;){
//
}
foreach($array as $value){
//
}
# constant
define(name, value, bool case-insensitive);
function test($y){
$GLOBALS['variable'] = 6; # 使用全局变量
static $x = 0; # 不删除局部变量
echo $y;
return $x;
}
# Regex
preg_match($pattern, $str); # return 1 if pattern match else 0
preg_match_all($pattern, $str) # return number of times matching
preg_replace($pattern, $str_replace, $str);
date(format[, timestamp]);
/*
* format:
* Y - year
* m - month
* d - day
* l - day of the week
* H - 24-hour format of hour
* h - 12
* i - minites
* s - seconds
* a - am or pm
*/
date_default_timezone_set("timezone");
mktime($hour, $minute, $second, $month, $day, $year); # return the Unix timestamp for a date
strtotime($time[, $now]); # convert a human readable string into Unix timestamp
include 'filename'; # warning if fail
require 'filename'; # fatal-error if fail
readfile("path"); # return file content
file_exists("path"); # return boolean, whether file exists
$f = fopen("path", "mode"); # return file object
fread($f, filesize("path"));
fgets($f); # read a single line
fgetc($f); # read a single char
feof($f); # check if EOF reached, return boolean
fclose($f);
/*
* mode:
* r
* w
* a, append
* x, create new file for write only
* + read/write
*/
// 上传文件
$_FILES["input name"]["name"];
$_FILES["input name"]["type"];
$_FILES["input name"]["size"];
$_FILES["input name"]["tmp_name"];
$_FILES["input name"]["error"];
/*
* <form action="upload_file.php" method="post" enctype="multipart/form-data">
* <input type="filel" name="input name" id="file">
* <input type="submit" name="submit" value="提交">
* </form>
*/
$from = $_FILES["input name"]["tmp_name"];
move_uploaded_file($from, $to);
// cookie
setcookie($name, $value, $expire, $path, $domain); # 必须位于<html>之前
$expire = time() + 3600; # 设置过期时间,单位s
$_COOKIE
// session
session_start();
// 存储 session 数据
$_SESSION['views']=1;
isset($_SESSION['views']); # 是否存在
unset($_SESSION['views']); # 销毁
session_destroy();
?>
|