# PHP

PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.

## Installation

Install the php package from the official repositories.

Note that to run PHP scripts as plain CGI, you need the php-cgi package.

## Running

While PHP can be run standalone, it is typically used with http servers such as Apache HTTP Server, nginx and lighttpd.

To run PHP standalone issue the `php -S localhost:8000 -t public_html/` command. See documentation.

## Configuration

The main PHP configuration file is well-documented and located at `/etc/php/php.ini`.

- It is recommended to set your timezone (list of timezones) in `/etc/php/php.ini` like so:

    date.timezone = Europe/Berlin

- If you want to display errors to debug your PHP code, change `display_errors` to `On` in `/etc/php/php.ini`:

    display_errors=On

- Remember to add a file handler for .phtml, if you need it, in `/etc/httpd/conf/extra/php5_module.conf`:

    DirectoryIndex index.php index.phtml index.html

Tip: If you are using php-composer it keeps its settings in a separate file in `/usr/share/php-composer/php.ini`

## Extensions

A number of commonly used PHP extensions can also be found in the official repositories:

    $ pacman -Ss php-

### gd

For php-gd uncomment the line in `/etc/php/php.ini`:

    extension=gd.so

### imagemagick

For imagemagick run `# pecl install imagick`. The pecl binary is included in the php-pear package. Then add

    extension=imagick.so

to `/etc/php/php.ini`.

### pthreads

If you wish to have POSIX multi-threading you will need the pthreads extension. To install the pthreads (http://pecl.php.net/package/pthreads) extension using `pecl` you are required to use a compiled version of PHP with the the thread safety support flag `--enable-maintainer-zts`. Currently the most clean way to do this would be to rebuild the original package with the flag.

Instruction can be found on the PHP pthreads extension page.

### mcrypt

If you want the `mcrypt` module, install php-mcrypt and uncomment the line in `/etc/php/php.ini`:

    extension=mcrypt.so

### PCNTL

PCNTL allows you to create process directly into the server side machine. While this may seen as something you would want, it also gives the power to PHP to mess things up really bad. So it is a PHP extension that cannot be loaded like other more convenient extension. This is because of the great power it gives to PHP. To enable it (by default it is disabled) PCNTL has to be compiled into PHP.

### MySQL/MariaDB

Install and configure MySQL/MariaDB as described in MariaDB.

Uncomment at least one of the following lines in `/etc/php/php.ini`:

    extension=pdo_mysql.so
    extension=mysqli.so

Warning: As of PHP 5.5, `mysql.so` is deprecated and will fill up your log files with error messages.

You can add minor privileged MySQL users for your web scripts. You might also want to edit `/etc/mysql/my.cnf` and uncomment the `skip-networking` line so the MySQL server is only accessible by the localhost. You have to restart MySQL for changes to take effect.

Tip: You may want to install a tool like phpMyAdmin, Adminer or mysql-workbench to work with your databases.

### PostgreSQL

Install and configure PostgreSQL, then install the php-pgsql package and uncomment the following lines in `/etc/php/php.ini`:

    extension=pdo_pgsql.so
    extension=pgsql.so

### Sqlite

Install and configure Sqlite, then install the php-sqlite package and uncomment the following lines in `/etc/php/php.ini`:

    extension=pdo_sqlite.so
    extension=sqlite3.so

## Caching

There are two kinds of caching in PHP: opcode/bytecode caching and userland/user data caching. Both allow for substantial gains in applications speed, and therefore should be enabled wherever possible.

While traditionally users turned to the Alternative PHP Cache (APC) for both needs, starting with PHP 5.5 APC was deprecated in favor of Zend OPCache, a more efficient opcode accelerator integrated to the core PHP distribution thanks to Zend releasing it as Libre software.[1]

However as OPCache provides only opcode caching, APCu was forked from the APC codebase to provide only userland caching while taking advantage of the many years of testing that had gone into it. For a combination of those two solutions, see OPCache + APCu.

Another notable contender is the actively maintained XCache, developed by one of Lighttpd authors and which provides both opcode and userland caching. See XCache.

### OPCache + APCu

OPCache comes bundled with the standard PHP distribution, therefore to enable it you simply have to add or uncomment the following line in your PHP configuration file:

    /etc/php/php.ini

    zend_extension=opcache.so

A list of its options and suggested settings can be found in its official entry on the PHP website.

Warning: If you choose to apply the suggested settings its manual offers, be sure to read carefully the first comment below those instructions as well. In some configurations those settings result in errors such as `zend_mm_heap corrupted` being produced.

APCu can be installed with the php-apcu package. You can then enable it by uncommenting the following line in `/etc/php/conf.d/apcu.ini`, or adding it to your PHP configuration file:

    extension=apcu.so

Its author recommends a few suggested settings, among which:

- `apc.enabled=1` and `apc.shm_size=32M` are not really required as they represent the default values;
- `apc.ttl=7200` on the other hand seems rather beneficial;
- finally, `apc.enable_cli=1`, which although not recommended by the manual may be required by some software such as ownCloud.

Tip: You can add those settings either to APCu's own `/etc/php/conf.d/apcu.ini` or directly to your PHP configuration file. Just make sure not to enable the extension twice as it will result in errors being diplayed in the system logs.

### XCache

XCache can be installed with the php-xcache package.

It may be enabled like APCu, by uncommenting its line in `/etc/php/conf.d/xcache.ini` or by directly adding it to the PHP configuration file.

## Zend Core + Apache

This article or section needs language, wiki syntax or style improvements.

Reason: Formatting and other style issues. (Discuss in Talk:PHP#)

Zend Core is the official PHP distribution provided by zend.com. It includes an installer/updater, zend optimizer, oracle support, and necessary libraries. However, it lacks support for postgresql, firebird, and odbc.

- Install mod_fcgid, a FastCGI module for Apache (the official one sucks).
- Install Zend Core (official PHP distribution)
  - Uninstall the php package.
  - Download and install zend core from [2] ; do not install the bundle apache or tell it to setup your web server. It always installs to `/usr/local/Zend/Core` due to hard-coded path.
  - Create a script `/usr/local/bin/zendcore` and create symlinks to php, php-cgi, pear, phpize under /usr/local/bin
    `#!/bin/bash
    export LD_LIBRARY_PATH="/usr/local/Zend/Core/lib"
    exec /usr/local/Zend/Core/bin/`basename $0` "$@"`
- Setup Apache:
  - In `/etc/httpd/conf/httpd.conf`, add
    `LoadModule fcgid_module modules/mod_fcgid.so
    <Directory /srv/http>
    AddHandler fcgid-script .php
    FCGIWrapper /usr/local/bin/php-cgi .php
    Options ExecCGI
    Allow from all
    </Directory>
    SocketPath /tmp/fcgidsock
    SharememPath /tmp/fcgidshm`
  - Remember to change the Directory path
- Disable Zend Optimizer (so you can use cache):
  - Edit `/etc/php.ini`, uncomment the following line near the end of file:
    `zend_extension_manager.optimizer="/usr/local/Zend/Core/lib/zend/optimizer"`

This article or section is out of date.

Reason: Use APCu/OPcache https://www.archlinux.org/news/php-55-available-in-the-extra-repository/ (Discuss in Talk:PHP#)

- Install APC (Alternative PHP Cache):
  - Run `pear install pecl.php.net/apc` as superuser.
  - Edit `/etc/php.ini`, add the line after "; Zend Core extensions..." (line 1205):
    `extension=apc.so`
- Update Zend Core and/or install other components
  - Just run `/usr/local/Zend/Core/setup`

## Development tools

### Komodo

Good integration for PHP+HTML+JavaScript. Lacks code formatting and unicode support in doc comments.

Komodo IDE | Komodo Edit (free)

Add custom encodings:

- Edit `KOMODO_INSTALL_DIR/lib/mozilla/components/koEncodingServices.py`, line 84, add:

     ('cp950', 'Chinese(CP-950/Big5)', 'CP950', '', 1,'cp950'),
     ('cp936', 'Chinese(CP-936/GB2312)', 'CP936', '', 1,'cp936'),
     ('GB2312', 'Chinese(GB-2312)', 'GB2312', '', 1,'GB2312'),
     ....

The format is (encoding name in python, description, short description, BOM, is ASCII-superset?, font encoding)

### Netbeans

A complete IDE for many languages including PHP. Includes features like debugging, refactoring, code tempalting, autocomplete, XML features, and web design and development functionalities (very good CSS autocomplete functionality and PHP/JavaScript code notifications/tips).

### Eclipse PDT

Eclipse PDT is not very complete at the current stage (v0.7); for instance, it cannot pop-up class list automatically when you type, though you can add custom auto-activation trigger keys.

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

### 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 through zendstudioAUR package in AUR.

### Aptana Studio

A good IDE for programming in PHP and web development. The current version (3.2.2) does not have a PHP debugger.

### Zend Code Analyzer

A PHP code analyzer from Zend Studio. The program is indispensable for any serious PHP coding.

#### Installation

- Download and install Zend Studio Neon
- In the installation dir, run `find . -name "ZendCodeAnalyzer` to get the path.
- Copy ZendCodeAnalyzer to `/usr/local/bin/zca`
- Now you can remove zend studio; you will not need a key or anything.

#### Vim Integration

Add the following lines into your `.vimrc`:

    autocmd FileType php setlocal makeprg=zca\ %<.php
    autocmd FileType php setlocal errorformat=%f(line\ %l):\ %m

#### Eclipse Integration

Error Link plugin:

- Symlink zca to build.zca (so Error Link can recognize it)
- Install Sunshade plugin suite;
- Preference -> Sunshade -> Error Link -> Add: `^(.*\.php)\(line (\d+)\): ()(.*)`
- Run -> External Tools -> Open External Tools Dialog -> Select "Program" -> Clicn on "New":
  Name: Zend Code Analyzer
  Location: /usr/local/bin/build.zca
  Working Directory: ${container_loc}
  Arguments: --recursive ${resource_name}

#### Komodo Integration

Toolbox -> Add -> New Command:

- Command: zca --recursive %F
- Run in: Command Output Tab
- Parse output with: `^(?P<file>.+?)\(line (?P<line>\d+)\): (?P<content>.*)$`
- Select Show parsed output as a list

## Troubleshooting

### 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

## See also

- PHP Official Website

Category:

- Programming languages