The Execution Operator

PHP uses backticks (') as its execution operator. Backticks are used very rarely in normal typing, so you might have trouble finding where yours is—it is usually to the left of the 1 key on your keyboard.

Backticks allow you to pass commands directly to the operating system for execution, then capture the results. PHP replaces the result of the execution with what you asked to be executed. For example:

    print 'ls';

That will run the command ls and output its results to the screen. If you are using Windows, you will need to use dir instead, as ls is only available on Unix. You can perform any commands inside backticks that you would normally perform directly from the command line, including piping output to and from and/or redirecting output through other programs.

There are several functions that perform program execution like the execution operator—you can find a more comprehensive reference to them in Chapter 7. Either way, you should be very wary about executing external programs from PHP because of potential security problems.

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.