开启 PHP 错误提示

修改原生 php 配置

修改 /etc/php.ini.default 的配置(macOS默认)。可通过 php -info , 找到 php.ini 配置文件路径.

  1. # 备份配置文件
  2. cp /etc/php.ini.default /etc/php.ini.default.backup
  3. # 编辑配置文件
  4. vim /etc/php.ini.default

编辑配置文件,查找并修改 display_errors 和 error_reporting 的值

// 找到 display_errors , 修改为 On
display_errors = On

// 找到 error_reporting
error_reporting = E_ALL & ~E_NOTICE 或者 error_reporting = E_ALL & ~E_DEPRECATED 
// 修改为 E_ALL | E_STRICT
error_reporting = E_ALL | E_STRICT

修改原生 apache 配置

修改 etc/apache2/httpd.conf 配置。

# 备份配置文件
cp etc/apache2/httpd.conf etc/apache2/httpd.conf.back

# 编辑配置文件
vim etc/apache2/httpd.conf

编辑配置文件,在最后添加如下配置

php_flag display_errors         on
php_value error_reporting       2039

重启 apache

sudo apachectl restart;

Phpstorm + Xdebug

输出 phpinfo 到文件

将下列代码保存为 php 文件, 如: phpinfo.php

<?php
# phpinfo.php

$myfile = fopen("phpinfo.txt", "w") or die("Unable to open file!");

# 打开输出缓冲区
# 所有的输出信息不直接发送到浏览器,而是保存在输出缓冲区
ob_start();
phpinfo();
$txt = ob_get_contents();
ob_end_clean();

fwrite($myfile, $txt);
fclose($myfile);

执行 phpinfo.php 文件。如果有多个版本的 php,则需要使用绝对路径。生成的 phpinfo.txt 文件在当前目录, 请自行处理

# 指定版本的PHP, 以homebrew安装的7.2版本为例
/usr/local/Cellar/php/7.2.11/bin/php phpinfo.php

安装 Xdebug

前往Xdebug 自定义安装, 在方框中填写 phpinfo.txt 文件的信息, 得到相应的安装教程

1. Download xdebug-2.6.1.tgz
2. Unpack the downloaded file with tar -xvzf xdebug-2.6.1.tgz
3. Run: cd xdebug-2.6.1
4. Run: phpize (See the FAQ if you don't have phpize.

As part of its output it should show:

    Configuring for:
    ...
    Zend Module Api No:      20170718
    Zend Extension Api No:   320170718
    If it does not, you are using the wrong phpize. Please follow this FAQ entry and skip the next step.

5. Run: ./configure
6. Run: make
7. Run: cp modules/xdebug.so /usr/local/lib/php/pecl/20170718
8. Edit /usr/local/etc/php/7.2/php.ini and add the line
    zend_extension = /usr/local/lib/php/pecl/20170718/xdebug.so

如果有多个版本的php。则需要相应的修改上述第4, 第5步的命令。需要指定对应版本的 php、php-fpm 配置文件路径

# 第4步, 以homebrew安装的7.2版本为例
/usr/local/Cellar/php/7.2.11/bin/phpize

# 第5步 (相应的路径信息可以从 phpinfo 的信息中找到)
./configure --with-php-config=/usr/local/Cellar/php/7.2.11/bin/php-config

错误处理

执行 make 时报了 “php.h file not found” 的错误,需要软链接 Xcode 开发工具

// 对应的系统版本为 "macOS 10.14"
sudo ln -s /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/ /usr/include

修改 php.ini

修改 php 配置,启用 xdebug,并配置 Step Debuging,配合 PhpStorm 断点调试

# 启用 xdebug
[Xdebug]
zend_extension = /usr/local/lib/php/pecl/20170718/xdebug.so

# 配置 Step Debugging
zend_extension = /usr/local/lib/php/pecl/20170718/xdebug.so
xdebug.remote_enable = On
xdebug.remote_handler = dbgp
xdebug.remote_host = localhost
xdebug.remote_port = 9000
xdebug.idkey = PHPSTORM