1 - Install PHP | Study PHP

Environment

  • macOS Sierra Version 10.12.4
  • Xcode: xcode-select --install
  • Homebrew

Install PHP

安装PHP:

1
2
3
4
5
6
7
8
9
# 快速安装php,参考 https://github.com/Homebrew/homebrew-php
> brew install brew-php-switcher
> brew install php56

> which php
/usr/local/bin/php

> which php-fpm
lrwxr-xr-x 1 leeco admin 32B May 17 15:08 /usr/local/bin/php -> ../Cellar/php56/5.6.30_6/bin/php

配置PHP:

1
2
3
4
5
6
7
# 配置 /private/etc/php.ini
> sudo vim /usr/local/etc/php/5.6/php.ini
# date.timezone = Asia/Shanghai

> php -i | grep timezone
Default timezone => Asia/Shanghai
date.timezone => Asia/Shanghai => Asia/Shanghai

安装 PEAR 和 PECL:

1
2
3
4
5
6
7
8
9
# 参考 https://jason.pureconcepts.net/2012/10/install-pear-pecl-mac-os-x/
> curl -O http://pear.php.net/go-pear.phar
> sudo php -d detect_unicode=0 go-pear.phar
# Press return

# 安装 intl 扩展,参考 http://note.rpsh.net/posts/2015/10/07/installing-php-intl-extension-os-x-el-capitan/
> sudo pear channel-update pear.php.net
> sudo pecl channel-update pecl.php.net
> sudo pear upgrade-all

如果要安装多个版本的PHP

1
2
3
4
5
# 用于在不同版本的php之间切换,参考 https://github.com/philcook/brew-php-switcher
> brew unlink php56
> brew install php71

> brew-php-switcher 56 -s # 切回 php 5.6

Install Composer

1
2
3
> brew install composer

> composer

Install PHP Extension

Install intl

安装

1
2
3
> brew install autoconf
> brew install icu4c
> brew install php56-intl # /usr/local/etc/php/5.6/conf.d/ext-intl.ini was created

检查是否安装成功:

1
> php -m | grep intl # 正常会包含 intl

Install OPcache

1
2
3
4
5
6
7
8
9
10
11
> brew install php56-opcache

> brew info php56-opcache
...
To finish installing opcache for PHP 5.6:
* /usr/local/etc/php/5.6/conf.d/ext-opcache.ini was created,
do not forget to remove it upon extension removal.
* Validate installation via one of the following methods:
...

> php -m | grep OPcache # 检查 OPcache 是否已生效

Install Xdebug

安装:

1
2
3
4
5
6
7
> brew install php56-xdebug
...
To finish installing xdebug for PHP 5.6:
* /usr/local/etc/php/5.6/conf.d/ext-xdebug.ini was created,
do not forget to remove it upon extension removal.
* Validate installation via one of the following methods:
...

The Xdebug extension will be enabled per default after the installation, additional configuration of the extension should be done by adding a custom ini-file to /usr/local/etc/php/<php-version>/conf.d/.

配置:

1
2
3
4
5
> sudo echo 'xdebug.remote_enable=1
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000
xdebug.profiler_enable=1
xdebug.profiler_output_dir="/tmp/xdebug-profiler-output"' >> /usr/local/etc/php/5.6/conf.d/ext-xdebug.ini

安装xdebug-osxxdebug开关工具):

1
2
3
4
5
6
7
8
9
10
11
12
13
> brew install xdebug-osx
...
Signature:
xdebug-toggle <on | off> [--no-server-restart]

Usage:
xdebug-toggle # outputs the current status
xdebug-toggle on # enables xdebug
xdebug-toggle off # disables xdebug

Options:
--no-server-restart # toggles xdebug without restarting apache or php-fpm
...

Use PHPStorm as IDE

Config Language & Frameworks

打开Preferences > Languages & Frameworks > PHP

添加CLI Intepreter

Config Xdebug

打开Preferences > Languages & Frameworks > PHP > Debug

如何配置参考Configuring Xdebug in PhpStorm

验证

创建一个PHP项目,新建一个php文件,创建执行配置:

打上断点,以Debug方式运行:

参考