Inroduction to Writing PHP Command-Line (CLI) Scripts

In this tutorial, I will show you how to get started with writing command-line interface (CLI) scripts using PHP. Here are a few reasons why you might want to write a CLI application in PHP:

  • You want to write a tool for admin-ing your server, but you’re more comfortable using PHP
  • You don’t need to output any content to a public-facing web page

The Basics

First, I’ll assume you’ve already got PHP setup on your web server and that you either know the path to the php executable or you’ve already added to your system’s PATH (you can test this by passing php –v to the CLI). If not, see this link.

When executing a command-line script, there are essentially three pieces to the command that is entered into the prompt.


> /path/to/php script.php arguments

/path/to/php: the path to your PHP executable.
script.php: the name of the PHP script you are executing
arguments: any commands, options, flags, or arguments you’ll be passing to the script

If you aren’t passing anything arguments to the script in the command-line, then what you see above is basically all you need to know to execute a script at the command-line. You’ve told the CLI what the interpreter will be for the command (/path/to/php) and the name of script to execute. Once you hit enter, the result should print directly to the screen.

Most users, however, will want to do more than just execute a standalone script with no external information passed to the script. For this, you’ll need to know how to read arguments passed to the script.

Reading Arguments in PHP from a CLI

PHP provides two important predefined variables that will make CLI scripting in PHP possible: $argv and $argc.


$argc [$argc on] is the easiest to get. It is simply the number of arguments passed to the script and it can be useful when determining whether or not a user entered a parameter.

*Note: The filename of the script is always passed as an argument to the script so the $argc count will always have a minimum value of 1.

Example 1


Example 2:

//Check to see if there were params passed to the script
if ( $argc > 1 ) {
  //do stuff here


$argv [$argv on] is the work horse of PHP CLI scripting and this is where you’ll be spending a lot of your time when writing interactive scripts. $argv is an array of arguments passed to the script.

Let’s look at some examples:

>  /path/to/php script.php arg1 arg2


array(2) {
    string(10) "script.php"
   string(4) "arg1"
   string(4) "arg2"

Notice that the first argument is always the name of the script.php that was executed.

Setting PHP ini variables in a CLI script


#!/usr/bin/php –d memory_limit=512M

  print "memory_limit=".ini_get("memory_limit")."\n";

There’s a lot thrown into this one, but let’s start at the beginning. In the linux/unix world, whenever the first two characters are “#!” (pound shebang), the system recognizes these as interpreter directives that alerts the system that this file is indeed a script and it should be interpreted by the path to executable after the directives (in our case: /usr/bin/php). Read more about the shebang.

On the same line, we’re passing the –d flag (which tells php we’re about set an INI config) and then of course, we’re setting the memory_limit to 512 Mb.

When the script is executed, it will read this line first and then pass the rest of the file to the interpreter.


array(1) {
  string(12) "./script.php"

How to accept user input in a PHP CLI script:

Here’s an example of how to write an interactive script that allows the user executing the script to enter in input during script execution.


  echo "Are you sure you want to do this?  Type 'yes' to continue: ";

  $handle = fopen ("php://stdin","r");
  $line = fgets($handle);

  if(trim($line) != 'yes'){
     echo "ABORTING!\n";

  echo "\n";
  echo "Thank you, continuing...\n";

To read the contents of input, you’ll need to use the php://stdin input stream.

In conclusion, if you’re beyond this beginner tutorial, I’d start by reading’s command-line features page. There are a lot more examples of how to do more involved things like looping through $argv and parsing the arguments to make the script perform as desired.

Published byJosh McGinnis

Josh is a software engineer, leader, startup advisor for the LA Chamber of Commerce and consultant residing in the Los Angeles area. Josh is passionate about helping entrepreneurs and businesses reach their maximum potential.


  • Andrew

    February 15, 2011 at 8:46 pm Reply

    Just thought you should know, your comment box is not working as desired. Whenever I click into the comment box, it clears the comment that I just wrote.

  • Andrew

    February 15, 2011 at 8:49 pm Reply

    Great intro to using PHP for command line scripts. One thing I would note is that $argc and $argv did not work for me, but $_SERVER[‘argc’] and $_SERVER[‘argv’] did. Probably something to do with my PHP configuration, but in case anybody else is having a similar problem, now you know.

  • PHP7 Thoughts

    November 23, 2016 at 6:56 pm Reply

    Really helped in understanding the programming of cli scripts and will try this php command on my test server.

Post a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.