# PHP (简体中文)

Related articles

- Pdepend
- Php-codesniffer-drupal
- PhpMetrics
- PHPLOC
- PhpDox

翻译状态： 本文是英文页面 PHP 的翻译，最后翻译时间：2016-09-07，点击这里可以查看翻译后英文页面的改动。

PHP是一种广泛使用的通用脚本语言，特别适合于Web开发，可嵌入到HTML。

## 安装

安装 从官方源安装php。AUR 中也提供了老的版本，例如 php56AUR.

注意：要想像纯CGI那样运行PHP，你需要安装 php-cgi 。

## 运行

虽然PHP可以独立运行，它通常用于HTTP服务器如： Apache HTTP Server(LAMP 组合), nginx, lighttpd 和 Hiawatha.

使用命令：“`php -S localhost:8000 -t public_html/` ”可以独立运行PHP。 见 documentation.

## 配置

主要PHP配置位于 `/etc/php/php.ini`.

- I建议在`/etc/php/php.ini` 中设置所在时区(list of timezones) 。如下:

    date.timezone = Europe/Berlin

- 如果你想调试PHP时显示错误，在`/etc/php/php.ini`中将`display_errors` 设为 `On`：

    display_errors=On

- open_basedir 限制 PHP 可以访问的目录，可以增加安全性。从 PHP 7.0 开始，和上游一样默认不再设置，要使用的用户请手动设置，例如：

    open_basedir = /srv/http/:/home/:/tmp/:/usr/share/pear/:/usr/share/webapps/

## 扩展

一些常用的PHP扩展也可以在官方库发现：

    $ pacman -Ss php-

Tip: 不要编辑`/etc/php/php.ini`，扩展的启停可在 `/etc/php/conf.d` 中设置，如： (e.g. `/etc/php/conf.d/gd.ini`)

要安装 PHP 老版本的扩展，可以在 AUR 中搜索 php56-*, 例如 php56-mcryptAUR。

### gd

欲使用 php-gd 在 `/etc/php/php.ini`中取消下列内容的注释:

    extension=gd.so

### imagemagick

运行`# pecl install imagick`安装imagemagick . pecl 包含于php-pearAUR 包. 在 `/etc/php/php.ini`中加入

    extension=imagick.so

### pthreads

要使用POSIX多线程，需要pthreads扩展 。要使用 `pecl` 安装 pthreads (http://pecl.php.net/package/pthreads) 扩展，需要 PHP 在编译时启用线程安全选项 `--enable-maintainer-zts`. 当前最简单的方式是用需要的选项重新编译.

可在 PHP pthreads extension 页面找到指令介绍。

### PCNTL

利用 PCNTL 可以在服务器上直接创建进程。虽然这可能是你想要的，但是这样也会让 PHP 有能力把机器搞的一团糟。所以 PHP 不能和其他扩展一样加载，要启用此扩展，需要重新编译PHP。ArchLinux 的 PHP 已经加入 "--enable-pcntl"选项，默认已经启用。

### MySQL/MariaDB

根据 MariaDB 页面安装并配置 MySQL/MariaDB.

取消 `/etc/php/php.ini` 中 下面行前面的注释 :

    extension=pdo_mysql.so
    extension=mysqli.so

警告: PHP 7.0 中 删除了 `mysql.so`。

可以给网络脚本最低的 MySQL 用户权限，可以编辑 `/etc/mysql/my.cnf` 取消 `skip-networking` 行的注释，这样 MySQL 服务器就只能本地访问。设置之后需要重启 MySQL。

### PostgreSQL

安装并配置 PostgreSQL，然后安装 php-pgsql 软件包并取消 `/etc/php/php.ini` 中下面几行的注释:

    extension=pdo_pgsql.so
    extension=pgsql.so

### Sqlite

安装并配置 SQLite，然后安装 php-sqlite 软件包并取消 `/etc/php/php.ini` 中下面几行的注释:

    extension=pdo_sqlite.so
    extension=sqlite3.so

### XDebug

用 XDebug 可以很容易的通过修改的 var_dump() 函数进行调试，安装 xdebug 并取消 `/etc/php/conf.d/xdebug.ini` 中如下行前面的注释：

    zend_extension=xdebug.so
    xdebug.remote_enable=on
    xdebug.remote_host=127.0.0.1
    xdebug.remote_port=9000
    xdebug.remote_handler=dbgp

### IMAP

安装 php-imap 并取消 `/etc/php/conf.d/xdebug.ini` 中如下行前面的注释:

     extension=imap.so

## 缓存

PHP有两种缓存： opcode/bytecode 缓存和userland/user data 缓存，这两种缓存都大幅度提升性能，因此最好开启。

- Zend OPCache仅提供opcode缓存。
- APCu仅提供userland缓存

要获得最佳性能，应当开启两种缓存。按照下面#OPCache和#APCu的步骤操作即可。

### OPCache

OPCache随PHP发布，因此在PHP configuration file[broken link: invalid section]中开启或添加下面两行即可：

    /etc/php/php.ini

    zend_extension=opcache.so

你可在官网 找到其他设置以及建议设置。

警告: 如果你使用推荐设置，要确保你一仔细看过说明，某些情况下可能导致如下错误：`zend_mm_heap corrupted`。

### APCu

通过 php-apcu 软件包安装 APCu, 然后在 `/etc/php/conf.d/apcu.ini` 中取消下面行的注释：

    extension=apcu.so

作者 建议进行一些设置:

- `apc.enabled=1` 和 `apc.shm_size=32M` 并不是必须的，因为这已经是 默认值;
- `apc.ttl=7200` 看上去 很有效;
- 最后, `apc.enable_cli=1`, 虽然手册上 不建议启用，有些软件比如 ownCloud 需要这个选项.

Tip: 可以将设置加入 APCu 自己的 `/etc/php/conf.d/apcu.ini` 或直接加到住配置文件，只需要注意不要同时加入。

## 开发工具

本页面需要更新翻译，内容可能已经与英文脱节。要贡献翻译，请访问简体中文翻译组。

附注: please use the first argument of the template to provide more detailed indications.

### Aptana Studio

Aptana Studio is an IDE for programming in PHP and web development. It can be installed with the aptana-studioAUR package. Does not have a PHP debugger as of version 3.2.2.

### Eclipse PDT

Eclipse PDT is the PHP variant of Eclipse. It can be installed with the eclipse-php package. See Eclipse for more information.

You would need other plugins for JavaScript support and DB query.

### Komodo

Komodo is an IDE with good integration for PHP+HTML+JavaScript. Komodo Edit is a free editor-only variant. 可以通过 komodo-editAUR 安装。

### Netbeans

NetBeans IDE is a complete IDE for many languages including PHP. Includes features like debugging, refactoring, code templating, autocomplete, XML features, and web design and development functionalities (very good CSS autocomplete functionality and PHP/JavaScript code notifications/tips). Install it with the netbeans package.

### PhpStorm

JetBrains PhpStorm is a commercial, cross-platform IDE for PHP built on JetBrains' IntelliJ IDEA platform. It can be installed with the phpstormAUR package, or with phpstorm-eapAUR for the 30-day trial version. You can get a free license for education from Jetbrains.[1]

### Zend Studio

Zend Studio is the official PHP IDE, based on eclipse. The IDE has autocomplete, advanced code formatting, WYSIWYG html editor, refactoring, and all the eclipse features such as db access and version control integration and whatever you can get from other eclipse plugins. You can install it with the zendstudioAUR package.

## Commandline tools

### Composer

Composer is a dependency manager for PHP. It can be installed with the php-composer[broken link: replaced by composer] package.

### Box

Box is an application for building and managing Phars. It can be installed with the php-boxAUR package.

### PDepend

PHP Depend (pdepend) is software metrics tool for php. It can be installed with the pdependAUR package.

### PHP Coding Standards Fixer

PHP Coding Standards Fixer a is PSR-1 and PSR-2 Coding Standards fixer for your code. It can be installed with the php-cs-fixerAUR package.

### PHP-CodeSniffer

PHP CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards. It can be installed with the php-codesnifferAUR package.

### phpcov

phpcov is a command-line frontend for the PHP_CodeCoverage library. It can be installed with the phpcovAUR package.

### phpDox

phpDox is the documentation generator for PHP projects. This includes, but is not limited to, API documentation. It can be installed with the phpdoxAUR package.

### PHPLoc

PHPLoc is a tool for quickly measuring the size of a PHP project. It can be installed with the phplocAUR package.

### PhpMetrics

PhpMetrics provides various metrics about PHP projects. It can be installed with the phpmetricsAUR package.

### phptok

phptok is a tool for quickly dumping the tokens of a PHP sourcecode file. It can be installed with the phptokAUR package.

### PHPUnit

PHPUnit is a programmer-oriented testing framework for PHP. It can be installed with the phpunitAUR package.

### PHPUnit Skeleton Generator

PHPUnit Skeleton Generator is a tool that can generate skeleton test classes from production code classes and vice versa. It can be installed with the phpunit-skeleton-generatorAUR package.

### Producer

Producer is a command-line quality-assurance tool to validate, and then release, your PHP library package.

It can be installed with the producerAUR package.

## 故障排除

### PHP Fatal error: Class 'ZipArchive' not found

Ensure the zip extension is enabled.

    $ grep zip /etc/php/php.ini

    extension=zip.so

### /etc/php/php.ini not parsed

If your `php.ini` is not parsed, the ini file is named after the sapi it is using. For instance, if you are using uwsgi, the file would be called `/etc/php/php-uwsgi.ini`. If you are using cli, it is `/etc/php/php-cli.ini`.

### PHP Warning: PHP Startup: <module>: Unable to initialize module

When running `php`, this error indicates that the aforementioned module is out of date. This will rarely happen in Arch Linux, since maintainers make sure core PHP and all modules be only available in compatible versions.

This might happen in conjunction with a module compiled from the AUR. You usually could confirm this by looking at the dates of the files `/usr/lib/php/modules/`.

To fix, find a compatible update for your module, probably by looking up the AUR using its common name.

If it applies, flag the outdated AUR package as outdated.

## 参见

- PHP 官方网站

Category:

- Programming languages (简体中文)

Hidden categories:

- Pages with broken section links
- Pages with broken package links