Chapter 3. The PHP Interpreter

This chapter discusses how PHP runs, both through the command line and through a web server, how PHP can be extended through built-in and third-party modules, and what can cause your scripts to terminate unexpectedly.

Running PHP Scripts

You can execute your scripts in one of two ways: through a web server where the output is sent to a web browser, or through the command-line interface (CLI) where the output is sent to standard output. Of the two, the former is more popular, but the latter is steadily growing in popularity.

The primary difference between outputting text to the command line and to a web browser is the format of new lines—through the CLI, you need to use \n for a new line, whereas for web browsers, you need to use the HTML line break, <br />. If you want to take a script designed for CLI and make it work through the Web, swap \n for <br />, and vice versa for converting web scripts to command line scripts.

If everything is configured properly, running scripts through your web server is as simple as putting the PHP script into your web server's public directory, then navigating to the appropriate URL with your browser. Running scripts through the command line is done using the CLI interpreter, which, if you are using Windows, is php.exe in the directory of your PHP installation. That is, if you have installed PHP into c:\php, the CLI program will be c:\php\php.exe. If you are using Unix, the availability of CLI PHP is down to how you ...

Get PHP in a Nutshell 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.