Chapter 18. Host Program Execution

In this lesson you will see how PHP allows you to execute programs on a host system and handle any output that is produced.

Executing Host Programs

PHP can call an external program that resides on a web server in a number of different ways. Let's look at them in the following sections.

The passthru Function

The simplest way to run a host command and display the output to screen is by using the passthru function. The command passed in as an argument is executed on the web server, and any resulting output is sent to the browser.

The following is a simple example that works on both Unix/Linux and Windows systems:

passthru("hostname");

The command hostname is executed on the host system, and its output is displayed. The ...

Get Sams Teach Yourself PHP in 10 Minutes now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.