BreadcrumbHomeResourcesBlog How To Use PHP Extensions March 31, 2020 How to Use PHP ExtensionsPHP DevelopmentModernizationBy Dmitry StogovPHP extensions are a critical part of the PHP development process, extending the functionality of the language to meet the needs of many different types of applications.In this blog, we give an introduction to PHP extensions for beginning PHP developers. Then we give more in-depth definitions for developers familiar with PHP extensions.Table of ContentsWhat Is A PHP Extension?What Is a .PHP File Extension?Where Do I Find Shared PHP Extensions?How to Use PHP ExtensionsDevelop Better PHP With ZendTable of Contents1 - What Is A PHP Extension?2 - What Is a .PHP File Extension?3 - Where Do I Find Shared PHP Extensions?4 - How to Use PHP Extensions5 - Develop Better PHP With ZendBack to topWhat Is A PHP Extension?A PHP extension is a specially formed library or plug-in that provides a function that can be used by many applications.Why Use PHP Extensions?By using extensions, you can avoid having to recreate the same code for numerous applications. To use an extension, you simply connect it to your application’s PHP binary. You can make use of a publicly available extension or create your own to: Provide functions and/or classes to work with data structures such as strings, arrays, objects, lists, and graphs.Implement bindings to some external libraries, such as POSIX regular expressions or simplexml.Connect to databases, such as MySQL or PostgreSQL.Increase security using Hashing or data encryption via technologies such as OpenSSL and Sodium.Facilitate processes with external services via technologies such as XML-RPC and SOAP.Add new capabilities to your application including caching, debugging, profiling, and testing.PHP extensions are usually written in C and then compiled to native machine code. However, PHP 7.4 introduced Foreign Function Interface (PHP FFI), which enables PHP developers to write extensions and bindings to C libraries in pure PHP. 💻Want to Develop Your Own PHP Extensions?💻Learn more about PHP extensions in our technical guide, including how to prepare a dev environment, generate an extension skeleton, install PHP extensions, and the advanced techniques needed to create PHP objects and classes with custom behaviors.See Our Free GuideBack to topWhat Is a .PHP File Extension?The term .php file extension refers to the name of a file with a PHP script or source code that has a ".PHP" extension at the end of it. It's similar to a Word file with a .doc file extension.PHP Extensions vs .PHP File ExtensionsPHP extensions are not the same thing as a .php file extension or PHP class extension (inherited classes). A PHP extension is a specially formed library or plugin. But a .PHP file extension is the name of a file. And a PHP class extension refers to a class that extends another.In other words, files that have a .php file extension are not the same thing as an extension a PHP developer uses in code to extend functionality. Back to topWhere Do I Find Shared PHP Extensions?If you have developed any code in PHP, you have already used extensions. Almost all the functions and classes provided by PHP come from different extensions. One of the simplest examples is the function strlen() implemented in “standard” extension. Another example is “mysqli_connect,” which is implemented in the “mysqli” extension.If your application needs some new functionality that is potentially useful for other applications, you should consider using a reusable library. You can add it by:Implementing this functionality in pure PHP, by writing one or more PHP classes.Using a ready-made extension that provides the functionality. Writing your own.PHP Extensions That Come With PHPIf you are lucky, you will find an extension that already provides the feature. There is a rich set of extensions maintained and distributed with PHP. For example, PHP 7.4 provides about 70 different extensions. Some of them are always compiled and linked directly into the PHP binary, like “standard.” These extensions are available out of the box. Other extensions are optional. Oftentimes, these are distributed as dynamic plug-ins that should be loaded though the php.ini extension directive — for example, extension=mysqli.so. Third-Party PHP ExtensionsDespite the extensions that come with the PHP core, you can find a lot of third-party extensions on the Internet. One of the biggest repositories — https://pecl.php.net/packages.php — contains almost 400 extensions. Not all of them are stable, portable among different PHP versions, and/or well supported. If you see an extension that could be useful, you need to try it to determine if it meets your requirements.Back to topHow to Use PHP ExtensionsBecause existing extensions don’t cover all the application domains, from time to time, you will need to develop a new extension. Companies that do business with PHP often develop extensions with very specific functionality. They also use extensions to re-implement mission-critical parts of code in C, to achieve better performance. Back to topDevelop Better PHP With ZendPHP extensions are just one aspect of PHP development. There's a lot else to consider, like debugging, security, deployment, performance, and integrations. PHP training can help you learn more. But using the right PHP development tools will help you development better applications. One of the best tools to use is Zend Server.You can use Zend Server to:Improve debugging.Boost security.Streamline deployments.Accelerate performance.Simplify integrations.See for yourself how Zend Server will help you. Get started with a free 30-day trial.Try Zend Server Additional Resources101 Guide: Developing Web Applications with PHPBlog: What Is PHP Foreign Function Interface?Blog: Managing Security Risks in the PHP Engine and Web ApplicationsBlog: Running SOAP WS-Security Load Tests in JMeterBack to top
Dmitry Stogov Principal Engineer, Zend by Perforce Dmitry started using PHP in 2000 and very soon turned into an internal PHP developer. He lead the Turck MMCache project for PHP-4 (which is similar to modern opcache). In 2003, Dmitry joined Zend and helped develop most of the core PHP subsystems during PHP-5's life. Later, Dmitry lead the PHPNG project, which brought the speed of PHP-7 to a new level and helped contribute to the release's success. Currently, Dmitry works at Zend by Perforce as a Principal Engineer and leads the development of the JIT compiler for PHP. He lives and works from his home in St. Petersburg, Russia.