Skip to content

Latest commit

 

History

History
64 lines (47 loc) · 2.32 KB

03-05-01-Command-Line-Interface.md

File metadata and controls

64 lines (47 loc) · 2.32 KB
isChild anchor
true
command_line_interface

Command Line Interface {#command_line_interface_title}

PHP was created to write web applications, but is also useful for scripting command line interface (CLI) programs. Command line PHP programs can help automate common tasks like testing, deployment, and application administration.

CLI PHP programs are powerful because you can use your app's code directly without having to create and secure a web GUI for it. Just be sure not to put your CLI PHP scripts in your public web root!

Try running PHP from your command line:

{% highlight console %}

php -i {% endhighlight %}

The -i option will print your PHP configuration just like the [phpinfo()][phpinfo] function.

The -a option provides an interactive shell, similar to ruby's IRB or python's interactive shell. There are a number of other useful [command line options][cli-options], too.

Let's write a simple "Hello, $name" CLI program. To try it out, create a file named hello.php, as below.

{% highlight php %}

php hello.php Usage: php hello.php [name] > php hello.php world Hello, world {% endhighlight %} * [Learn about running PHP from the command line][php-cli] * [Learn about setting up Windows to run PHP from the command line][php-cli-windows] [phpinfo]: http://php.net/function.phpinfo [cli-options]: http://php.net/features.commandline.options [argc]: http://php.net/reserved.variables.argc [argv]: http://php.net/reserved.variables.argv [exit-codes]: http://www.gsp.com/cgi-bin/man.cgi?section=3&topic=sysexits [php-cli]: http://php.net/features.commandline [php-cli-windows]: http://php.net/install.windows.commandline