PHPDeveloper

Running PHP from the Command Line - Basics

by Jarrod posted 5 years ago
Running PHP from the Command Line - Basics

Here are a few of tips to share with you regarding running your PHP from the command line.

Note: this is article is intended for Linux users, although it should work with other terminals from other OS's.

Running your PHP from the command line

That's as easy as calling "php" then your script name:

$ php script_name.php

Accepting arguments

You can pass arguments, also referred to as variables, to your script from the command line like so:

$ php script_name.php Jarrod

This example passes one argument, "Jarrod". To do something useful with this in PHP we use the array variable $argv. This is simply an array, created by PHP, that contains our arguments.

if (isset($argv[1])) {
    echo 'My name is ' . $argv[1];
}

Notice we used the array index 1 and not 0? That is because $argv[0] is always the name that was used to run the script.

You can use multiple arguments, each argument will be a new array element.

$ php script_name.php Jarrod "cake, tea and ice cream"

Just to claify, the two arguments used above are:

  1. Jarrod
  2. cake, tea and ice cream

The second argument has spaces so I used quotes around the phrase.

if (isset($argv[1]) && isset($argv[2])) {
    echo 'My name is ' . $argv[1] . ' and I like ' . $argv[2];
}
A line break would be good...

New Lines

Want to add a line break? <br /> won't work. Instead, you need to use the constant PHP_EOL.

if (isset($argv[1])) {
    echo 'My name is ' . $argv[1] . PHP_EOL;
}

Colors

By prefixing a color code to your string you can change the text color. A color code looks like this for the color red: \033[1;31m (it's actually the last part that contains the color - in this example it's the 31 - but you still need the whole thing).

if (isset($argv[1])) {
    echo "\033[0;31mMy name is " . $argv[1] . " and I like " . $argv[2] . "\033[0;30m";
}

A few points to make:

  1. There is no space between the color and my text. You can add a space - but the space will show up.
  2. At the end I include a color code. This is not like a closing tag, it's to reset the color back to black.
  3. I changed my single quotes to double quotes so the color code is actually parsed and not treated like a string!

Here are some common colors:

  • Black = \033[0;30m
  • Red = \033[0;31m
  • Green = \033[0;32m
  • Yellow = \033[0;33m
  • Blue = \033[0;34m
  • Purple = \033[0;35m
  • Cyan = \033[0;36m
  • White = \033[0;37m

Style

Similar to the color code - in fact it's the same thing, we just change a different part -you can set the style of the font; like bold, underline, etc.

This is done by changing the value shown in bold: \033[0;31m

A list of possible options are:

  • 0 = normal
  • 1 = bold
  • 4 = underline
  • 5 = blink
if (isset($argv[1])) {
    echo "\033[5;31mMy name is " . $argv[1] . " and I like " . $argv[2] . "\033[0;30m";
}

Comments for Running PHP from the Command Line - Basics

  • Donald Duck
    Michael Rosata 2015-02-06 18:39:53

    Nice little "up quick", the tutorial moved at a good speed. I was looking for a couple quick tips on the CLI and that's what I found. Thanks

  • Donald Duck
    Ngoma Gil 2015-02-25 11:15:45

    Thanks for the tutorial. I want to know to ask user input. Thanks.

  • Donald Duck
    Chris Knox 2015-09-22 15:11:00

    I need to test my PHP installation. This is the closest I've seen to what I'm trying to accomplish. I have a little test script named php-test.php and and php is installed in c:\PHP. It has two lines: echo "param = ".$param."\n"; var_dump($argv); If I run it from the command line without qualification, I get the code source: C:\PHP\cmdbParser>php php-test.php echo "param = ".$param."\n"; var_dump($argv); I run it from teh command line, unqualified with a parameter and I get the same thing. C:\PHP\cmdbParser>php php-test.php blah echo "param = ".$param."\n"; var_dump($argv); Finally, if I run it with a fully qualified path, it echoes the version: C:\PHP\cmdbParser>c:\php\cmdbparser\php-test.php X-Powered-By: PHP/5.6.13 Content-type: text/html; charset=UTF-8 echo "param = ".$param."\n"; var_dump($argv); Any thoughts on this at all are welcome.

  • Donald Duck
    Elena 2015-12-15 05:44:11

    So I have this : $dirWithMovies = $_SERVER[ "argv " ] [ 1 ] ; $outFi le = $_SERVER[ "argv " ] [ 2 ] ; i s _di r ( $dirWithMovies ) or die ( $dirWithMovies . " i s not a di rectory . \n" ) ; And on command line it gives me output "is not a directory", which means that I havent passed $_SERVER["ARGV"][1] an argument which should be a whole directory, right? How can I do that with Command Line? Thanks in advance. Elena

  • Donald Duck
    we 2017-03-06 22:26:58

    wewe

  • Donald Duck
    small business internet 2017-04-02 10:39:06

    Hey there! Would you mind if I share your blog with my zynga group? There's a lot of folks that I think would really enjoy your content. Please let me know. Cheers

Type Your Comment