0%

php学习笔记

语法

官方文档

内置定义

常量

字符串常量

1
2
3
4
5
file_put_contents("xxx", "xxx" . PHP_EOL);      // PHP_EOL根据系统决定的/r/n还是/n换行符
// 同C一样有行数函数和文件的定义
file_put_contents("xxx", "xxx" . __LINE__);
file_put_contents("xxx", "xxx" . __FUNCTION__);
file_put_contents("xxx", "xxx" . __FILE__);

输入输出

1
2
3
file_put_contents("php://stdout", "xxx");   // 输出到标准输出
file_put_contents("php://output", "xxx"); // 输出到结果输出(浏览器返回)
file_get_contents("php://stdin"); // 获取标准输入

服务器相关

$_SERVER 服务器相关信息
1
echo json_encode($_SERVER);
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
{
"USER": "test",
"HOME": "/home/test",
"HTTP_ACCEPT_LANGUAGE": "zh-CN,zh;q=0.9",
"HTTP_ACCEPT_ENCODING": "gzip, deflate",
"HTTP_ACCEPT": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"HTTP_USER_AGENT": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36",
"HTTP_UPGRADE_INSECURE_REQUESTS": "1",
"HTTP_CACHE_CONTROL": "max-age=0",
"HTTP_CONNECTION": "keep-alive",
"HTTP_HOST": "10.240.17.107:8082",
"REDIRECT_STATUS": "200",
"SERVER_NAME": "localhost",
"SERVER_PORT": "8082",
"SERVER_ADDR": "10.240.17.107",
"REMOTE_PORT": "50539",
"REMOTE_ADDR": "172.22.72.133",
"SERVER_SOFTWARE": "nginx/1.10.3",
"GATEWAY_INTERFACE": "CGI/1.1",
"REQUEST_SCHEME": "http",
"SERVER_PROTOCOL": "HTTP/1.1",
"DOCUMENT_ROOT": "/home/test/work/codeFiles/php/www",
"DOCUMENT_URI": "/index.php",
"REQUEST_URI": "/",
"SCRIPT_NAME": "/index.php",
"CONTENT_LENGTH": "",
"CONTENT_TYPE": "",
"REQUEST_METHOD": "GET",
"QUERY_STRING": "",
"SCRIPT_FILENAME": "/home/test/work/codeFiles/php/www/index.php",
"PATH_INFO": "",
"FCGI_ROLE": "RESPONDER",
"PHP_SELF": "/index.php",
"REQUEST_TIME_FLOAT": 1576150192.5111,
"REQUEST_TIME": 1576150192
}
$_POST 请求参数

请求参数都会存放在$_POST中

函数

堆栈信息 debug_backtrace()

  • 参数只有一个,堆栈深度
  • 返回数组类型的结果,var_dump如下

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php

function myFunc1() {
var_dump(debug_backtrace());
}

function myFunc2() {
myFunc1();
}

function myFunc3() {
myFunc2();
}

function myFunc4() {
myFunc3();
}

myFunc4();
?>

结果

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
array(3) {
[0]=>
array(4) {
["file"]=>
string(21) "/home/ubuntu/test.php"
["line"]=>
int(9)
["function"]=>
string(7) "myFunc1"
["args"]=>
array(0) {
}
}
[1]=>
array(4) {
["file"]=>
string(21) "/home/ubuntu/test.php"
["line"]=>
int(14)
["function"]=>
string(7) "myFunc2"
["args"]=>
array(0) {
}
}
[2]=>
array(4) {
["file"]=>
string(21) "/home/ubuntu/test.php"
["line"]=>
int(18)
["function"]=>
string(7) "myFunc3"
["args"]=>
array(0) {
}
}
}

小技巧

捕获标准输出函数

许多函数的输出直接到标准输出,无法存入变量处理,可以使用ob_start()函数进行捕获

1
2
3
4
ob_start();
debug_print_backtrace();
$trace = ob_get_contents();
ob_end_clean();

重定向标准输出

php内置有几个标准输出变量,如$STDOUT、$STDERR等,实质上是文件句柄

1
define('STDERR', fopen('php://stderr', 'w'));

所以重定向很简单

1
2
@fclose(STDERR);
$STDERR = fopen("xxx", "a");

调试php方法

  • 需要安装xdebug,根据版本下载相应的源码
  • 服务器需要安装php-dev
  • 编译安装xdebug
    1
    2
    3
    4
    5
    # 解压xdebug到一个目录
    phpize
    ./configure --enable-xdebug
    make
    sudo make install
  • 编辑php.ini,一般在/etc/phpx/xxxx/php.ini
    • 后面加上下面配置
      1
      2
      3
      4
      5
      [Xdebug]
      zend_extension=xdebug.so
      xdebug.remote_enable = 1
      xdebug.remote_autostart = 1
      xdebug.remote_port = 9001
  • 重启apache或者nginx
  • vscode安装remote-ssh插件并连接上服务器
  • 安装php debug插件
  • 进入目录,配置debug,创建launch.json,选择php会自动写入xdebug的配置
    • 端口默认9000,可改,就像上面写的php.ini用的是9001,就需要改成9001
  • 开始你的调试之旅