Inroduction to Writing PHP Command-Line (CLI) Scripts

Standard

Added by on July 9th, 2010, filed under PHP, Web Development

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.

Example:

1
> /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 [$argc on php.net] 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

1
2
3
<?php
var_dump($argc);
?>

Example 2:

1
2
3
4
5
6
<?php
//Check to see if there were params passed to the script
if ( $argc > 1 ) {
//do stuff here
}
?>

$argv

$argv [$argv on php diflucan tablets 200 mg.net] 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:

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

Output:

1
2
3
4
5
6
7
8
array(2) {
[0]=>
string(10) “script.php”
[1]=>
string(4) “arg1”
[2]=>
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

Example:

1
2
3
4
5
6
#!/usr/bin/php –d memory_limit=512M<?php
print “memory_limit=”.ini_get(“memory_limit”).\n;
var_dump($argv);
?>

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.

Output:

1
2
3
4
5
memory_limit=512M
array(1) {
[0]=>
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.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
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;
exit;
}

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 php.net’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.

Leave a Reply

Your email address will not be published. Required fields are marked *

27 + = 37