Get Command-Line Arguments With PHP $argv or getopt() Read Time: 7 mins Any PHP code that we write is usually run on the server when users request a webpage. However, it is also possible to run a script through a CLI or command-line interface. Sometimes it becomes necessary to write PHP scripts that can accept arguments when executed inside a command prompt. There can be a variety of reasons for doing this, ranging from ease of use to automation of specific tasks. In this tutorial, you will learn how to access command-line arguments inside your script with $argv and getopt() in PHP.

Why Do We Need Them?

Arguments are passed around as query parameters when you request a page from your web browser. On the server side, we access the values inside these parameters using the superglobal $_GET. Unfortunately, this technique won't work when you are running your script inside the command prompt. This is because a command prompt expects arguments for its script in a different format. Let's create a simple script to better understand the problem. Put the following code inside a file called reverse.php. Now, try to run the following command from the directory in which the file reverse.php is located. This will give you an error similar to the following line. This happens because the system tries to run a file called reverse.php?n=monty within the current directory, but there is no such file. You can run the following command successfully, but it still won't output the reversed name. It will show a warning about an undefined array key "n" instead. The use of $argv and getopt() solves this problem for us.

Get Command-Line Arguments With $argv There are two predefined variables in PHP, called $argc and $argv, that you can use to work with command-line arguments. The variable $argc simply tells you the number of arguments that were passed to the script. Remember that the name of the script you are running is always counted as an argument. This means that the minimum value of $argc would be 1. The variable $argv is much more helpful because it is an array of arguments passed to the script and contains their actual value. You can iterate over all the values with a foreach loop.

Let's create a script to change the brightness and contrast of JPEG images. The script will accept three parameters besides its name: the name of the file, desired brightness, and desired contrast. Put the following code in a file called im-filters.php.

The $filter_list string is used to keep track of all the filters applied to the image to show them to the user. Try running the following command in the console now. It should show you the following output: Avoiding Script Duplication We have seen that different code needs to be used to access argument values when the script runs on a web server vs. when it runs in a CLI. However, this does not mean that you will require two copies of the same script to run in different environments. There is a function called php_sapi_name() that will tell you whether your code is running inside a web server or a CLI. The predefined constant PHP_SAPI serves the same purpose. Final Thoughts There are a lot of times when we have to write code that will work on some provided input to give us an output. Usually, the input is handled by $_GET and $_PUT when the script is running on a server. However, you will sometimes need to run your script through a CLI, and that requires you to use $argv or getopt(). Using getopt() gives you more flexibility when dealing with the arguments and does some heavy lifting regarding their parsing for you.

Try running the above script on your own images while changing the order of arguments. You should consider creating your own custom scripts to automate boring tasks with a task scheduler.