ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.

Advertisements

PHP on the command line

(Page 1 out of 2)

Introduction

PHP is traditionally only used on the internet to power our websites, but it's also capable of doing more. Since version 4.3 PHP ships with a special version which can be used to execute scripts on the command line, and do certain system tasks. If you're a Linux user, you probably know what the command line is, but if you're a Windows user you might not.

The command line is used to directly execute certain system commands. To access the command line on Windows XP, go to Start -> Run, and then type 'cmd.exe'. A new DOS prompt will pop-up, and a line will be blinking after C:\Windows. Something like this:

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

D:\Documents and Settings\Kings>_

This is the command line, and you can now enter commands to do certain things. But it's also possible to execute PHP scripts with the PHP command line engine, called PHP CLI.

In this tutorial I will show you how to use the PHP CLI, its features, and what it can be used for. Let's get started with the most basic thing: how to use PHP CLI.

How to use PHP CLI

To run a PHP script on the command line in Linux you have to CHMOD the PHP script executable ("chmod +x script.php") and it can then be used just like a program. Simply click the script, and it will run on the command line.

In Windows however it's a bit trickier. To run a PHP script on the command line, first open the command prompt (Start -> Run, 'cmd.exe'). Then browse to the directory in which your script is located using standard DOS commands. For example, if my script is located in C:\myscripts, I'd type the following

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

D:\Documents and Settings\Kings>cd..

D:\Documents and Settings>cd..

D:\>cd myscripts

D:\myscripts\_

When you're in the correct directory, first enter the location to your CLI php.exe (usually C:\PHP\CLI\php.exe), then a space, and then your script name, e.g.

C:\PHP\CLI\php.exe script.php

Then hit enter to run your script on the command line.

Let's try it with a simple Hello World example:

#!/usr/bin/php

        echo 'Hello World';
?>

Note the first line in the above example. This is called a shebang, and is only useful for Linux, as it tells the command line interpreter where to find the PHP executable (make sure it's correct), and it's ignored by Windows.

If you run the above example, you will get the following result:

Content-type: text/html
X-Powered-By: PHP/4.4.0

Hello World

As you can see it prints the 'Hello World', but also two other lines before that. These are standard headers sent out by PHP when it's used normally, but they're completely useless now. This is no problem though, as it's fairly simple to hide them. For Linux users, you have to change the shebang to:

#!/usr/bin/php -q

And Windows users have to use the -q as an argument, like so:

G:\Projects\PHPit\content\php on the command line\demos>D:\PHP\CLI\php.exe -q simple.ph
p.

That's it for the basic use of PHP CLI scripts, so let's have a look at a special CLI feature: arguments.

Next: Arguments & STDIN »



3 Responses to “PHP on the command line”

  1. Richard@Home » Blog Archive » links for 2006-03-14 Says:

    […] PHPit - Totally PHP » PHP on the command line A tutorial covering how to run php scripts from the command line. (tags: php cmd) […]

  2. PHPit - Totally PHP » An Introduction to the Winbinder Library, Part 1 Says:

    […] PHP is used for many different things, and almost anything is possible, including command line scripts (see a recent article on PHPit on command line script). But it’s also possible to create full-blown Windows applications using PHP and the Winbinder library, and that’s what we’ll be looking at in this new article series. […]

  3. Dion Says:

    If you want an alternative for Unix/Linux’s #! syntax, take a look here:
    http://whitescreen.nicolaas.net/php_polyglot.php

Leave a Reply

About the author
Dennis Pallett is the main contributor to PHPit. He owns several websites, including ASPit and Chill2Music. He is currently still studying.
Article Index
  1. The Basics
  2. Arguments & STDIN
Bookmark Article
Download Article
PDF
Download this article as a PDF file