blog image what is php used for
August 26, 2020

PHP Basics: What Is PHP Used For?

PHP Development

PHP is used for web applications and APIs, and, with its robust command line interface and extensions such as Swoole, can also be used to write systems scripts and create network daemons.

PHP was initially developed in the mid-1990s for serving dynamic web pages: pages that pulled content from the filesystem or the database, instead of being static, hard-coded pages. While other technologies existed at the time, PHP gained popularity due to its ease of deployment and the ease with which developers could write it. Over the years, it has faced competition from new and old technologies alike, but still remains the most-used language for websites, powering 72-80% of websites.

In the next few sections, we'll look at how PHP is used today — with details on how it's used in websites, web APIs, queue and background task processing, and web servers.

Back to top

PHP Use Case #1: Websites

Still arguably the most common use case for PHP is to deliver a website that provides dynamic content. PHP provides extensions for connecting to all of the most popular open source and proprietary databases commonly used today, including MySQL, PostgreSQL, Oracle, DB2, and SQL Server. Additionally, via its community extension library, PECL, you can install extensions that connect to NoSQL data stores, caching services, and more. These capabilities allow you to serve up custom content, individually tailored for the user requesting it.

Back to top

PHP Use Case #2: Web APIs

Originally designed for inter-server communications, web APIs have exploded in use in recent years in large part due to technologies such as mobile, Internet of Things (IoT) devices, and more.

Web APIs allow for flexibility in deployment, and also allow leveraging existing technologies to perform common operations such as authentication, authorization, rate limiting, and more, that are built in to web servers and web application frameworks.

PHP has always had strong XML support, which has often been used as a message format for APIs. Additionally, it was one of the first languages to provide native JSON support, which has quickly become the lingua-franca of Web APIs due to the ease and safety of parsing it both on servers as well as in client applications such as browsers and mobile devices.

Couple these capabilities with PHP's wealth of support for data backends, and you have an ideal language both for consuming and providing web APIs.

Related Blog >> What Are APIs?


Back to top

PHP Use Case #3: Queue / Background Task Processing

Though PHP remains primarily a language for the web, it has evolved tremendously over the years, and has provided a command line interface (CLI) since version 4.2.0. This allows you to execute scripts without the need for a web server, which can be useful in a variety of scenarios:

  • During deployment, to perform tasks such as pre-caching, migrations, etc.
  • For scheduled jobs (e.g., via the crond daemon).
  • As a message queue worker.

Because the CLI interface has access to all the same features as when you use it for web applications, you can connect to the same data sources as you would normally. This can be incredibly powerful, as you can use the same code both in your web application, as well as when you need to do something such as database maintenance, processing uploads, and more.

Back to top

PHP Use Case #4: Web Servers

PHP itself offers a rudimentary built-in web server, which can be useful during development to allow testing functionality without requiring a full-blown web server and everything that entails.

If you want even more power, some extensions and libraries allow you to use PHP to create network servers, including full-blown, production-ready web servers! Such capabilities generally require the ability to create an asynchronous event loop — a technology seen in most web servers, but also in languages such as Node.js.

One such extension to PHP is Swoole. Swoole provides many of the features of Node.js, as well as a number of additional features:

  • Multiple web workers.
  • A separate task worker pool for deferring execution of tasks outside of the web worker pool; this essentially provides a built-in message queue.
  • Coroutine support for several extensions, as well as most TCP/UDP operations. This means you can write normal code that benefits from the performance improvements of an async runtime.

These sorts of capabilities allow you to create single-purpose, self-contained containers that provide web applications or APIs. This latter is particularly intriguing when you are creating suites of microservices, and may want the ability to horizontally scale your architecture.

Back to top

Final Thoughts

The question in 2020 isn't so much what you can use PHP for, but what you will use PHP for. With PHP's ability to create web applications and APIs, and its robust command line interface and extensions like Swoole, developers can create a wide range of useful scripts all while using the language they have already grown to love for the web.

Need Help on Your Next PHP Project?

With expert guidance from our PHP experts, Zend can help you translate your web application and website ideas into reality. Ready to see what we can offer? Click the link below to speak with a PHP expert today!

Speak With a PHP Expert

Additional Resources

Want to see how developers are using PHP today, and predictions for the future? This webinar is a must-watch.

Looking for further reading on PHP? These resources are worth your time.

Back to top