How do i pass a command line argument in php?
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. Show
In this tutorial, you will learn how to access command-line arguments inside your script with 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 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. php reverse.php?n=monty This will give you an error similar to the following line. Could not open input file: reverse.php?name=monty 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. php reverse.php monty It will show a warning about an undefined array key "n" instead. The use of Get Command-Line Arguments With $argvThere are two predefined variables in PHP, called The variable The variable 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. We create an array with three elements and autofill it with Now, run the following command in the terminal: php im-filters.php beach.jpg -40 -20 This should create the following output in the terminal, while generating a file called beach-edited.jpg. Edited Image: beach.jpg (Brightness: -40 and Contrast: -20) Get Command-Line Arguments With getopt()There are a few shortcomings in the method that we used to access command-line arguments in our previous example. For instance, we are assuming that the arguments will be provided in the order in which we are processing them in the script. However, that might not always be the case. The
Both
One thing you should remember is that the array returned by Now it's time to create our script with all these points in mind. We accept four different arguments for our image editing script and have set only the filename to be required. The grayscale filter does not accept any parameter, so we have marked it as such. We check if the value has been supplied with a long-form or short-form option for each of our arguments. The code inside the The Try running the following command in the console now. php im-filters.php --filename="beach.jpg" --grayscale -c-20 --brightness="-30" It should show you the following output: Edited Image: beach.jpg Grayscale Brightness:-30 Contrast:-20 Avoiding Script DuplicationWe 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 Final ThoughtsThere 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 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. Did you find this post useful? Freelancer, Instructor I am a full-stack developer who also loves to write tutorials. After trying out a bunch of things till second year of college, I decided to work on my web development skills. Starting with just HTML and CSS, I kept moving forward and gained experience in PHP, JavaScript and Python. I usually spend my free time either working on some side projects or traveling around. How do I run a command line argument in PHP?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.
How do you pass command line arguments?If you want to pass command line arguments then you will have to define the main() function with two arguments. The first argument defines the number of command line arguments and the second argument is the list of command line arguments.
Can we use PHP to write command line script?There are two variables you can use while writing command line applications with PHP: $argc and $argv.. Telling PHP to execute a certain file. ... . Pass the PHP code to execute directly on the command line.. How do I pass a command line argument in command prompt?option. You can test command line arguments by running an executable from the "Command Prompt" in XP, Vista or later, or from the "DOS prompt" in older versions of Windows. You can also use command line arguments in program shortcuts, or when running an application by using Start -> Run.
|