2.8. Extensions

The PHP interpreter is built on a modular basis. The language consists of a kernel to which extensions can be connected if necessary — libraries that supplement the language with new features.

For example, for image processing you can connect the GDLib extension, for connection with databases – the PDO extension, for support of multibyte strings (UTF-8) – mbstring. Some extensions have become so widespread in popularity that they were included in the kernel. For example, these are string functions from the calendar extension or support for sessions from the session extension.

Attention! The material of the current section can be skipped at first reading.

Each extension increases the amount of RAM that the PHP interpreter occupies. The more memory the interpreter consumes, the fewer PHP processes you can run on the server, the fewer connections the server can serve in one second. Therefore, extensions are connected and disconnected as needed. In order to reduce the size of the PHP executable file and, consequently, the amount of RAM consumed, most extensions are not included in the PHP core. Instead, they are compiled as external dynamic libraries: *.dll for Windows and *.so for UNIX-like operating systems.

The easiest way to find out if an extension is connected is to run the php -m command, which lists the extensions available. You can also use the phpinfo() function report (see listing 2.2). In the Configuration section, the attached extensions are indicated, and the tables that accompany each of the extensions provide a list of the parameters for this extension. Most of these parameters can be adjusted in the php.ini configuration file.

In distributions for the Windows operating system, extensions designed in the form of dynamic libraries are compiled but not connected. You can find them in the ext subdirectory of the main folder of the PHP distribution:

php_bz2.dll
php_corn_dotnet.dll
php_curl.dll


php_tidy.dll
php_xrnlrpc.dll
php_xsl.dll

In order to connect one of these external extensions, you need to edit the configuration file php.ini. The path to it can be found in the report function phpinfo(). In the php.ini configuration file, find the extension_dir directive and specify the path to the folder with extensions in it:

extension dir = “ext”

After the path to the folder with the extension is specified, you can activate the extensions themselves by using the extension directive. In the configuration file php.ini, as a rule, commented directives for all extensions from the ext folder have already been added. The necessary extension must be activated by removing the semicolon from the beginning of the line:

extension=php_mbstring.dll

В случае UNIX-подобных операционных систем установка расширений осуществляется еще проще. Как правило, они оформлены в виде отдельных пакетов. Так в Mac OS X, в основном, используется менеджер пакетов Homebrew. В разделе 2.2 PHP устанавливался при помощи команды:

$ brew install php71

In the same way, an extension is installed, only the name of the php71 package is supplemented with the name of the extension, for example, for CURL, the command might look like this:

$ brew install php71-curl

In modern Linux distributions, the installation of extensions is also done using the package manager, in the case of Ubuntu it is apt-get. Recall that installing PHP is done by the command:

$ sudo apt-get install php7.0

Extensions are installed the same way. So, CURL can be set using the command:

$ sudo apt-get install php7.0-curl