Loading...
X

How to run PHP script on the command line (without web server)

How to run PHP script on Windows command line

If you want not only the ability to run PHP from the command line, but also a full-fledged Apache + PHP + MySQL web server on Windows, then complete the installation according to this article.

To run PHP on the command line, you do not need to install a web server, just download and unpack the archive with the PHP interpreter.

Let's start by downloading the latest PHP version for Windows from the official website: https://windows.php.net/download/

There are several options that differ:

  • Version (e.g. 8.0, 7.4, 7.3)
  • Computer architecture, (x64 and x86)
  • Thread Safe or Non Thread Safe

Choose the latest version, between x64 and x86 choose the same version as your webserver. That is, if your Apache is 64-bit, then PHP must be 64-bit as well. Always choose ‘Thread Safe’ version.

There are two links for each file:

  • Zip
  • Debug Pack

Choose Zip because the debug package is only for those who really know why they need it. This does not mean debugging PHP scripts, but debugging the PHP interpreter itself.

If you often run PHP scripts from the Windows command line, it is highly recommended to To add PHP path to %PATH% environment variable on Windows. This eliminates the need to specify the full path to the php.exe file every time.

Now that PHP is installed and the path to php.exe has been added to the Windows environment variable, open a command prompt by pressing the Win+x key combination and select Windows PowerShell.

To check what works fine, look at the PHP help:

php -h

In fact, we are running the php.exe file, but the extension can be dropped. That is, the previous entry is equivalent to

php.exe -h

How to run .php script on Windows and Linux command line

The following command is used to run the .php file in the Windows console:

php -f path\to\file.php

The -f option can be skipped, that is, the previous and next commands are the same:

php path\to\file.php

I created a test file which is located in the path C:\Users\Alex\Documents\PHP\test.php then I can run it in PHP like so:

php -f C:\Users\Alex\Documents\PHP\test.php

or like this:

php C:\Users\Alex\Documents\PHP\test.php

How to pass arguments to a PHP script on the command line

To pass arguments to the script, list them after the filename, separated by a space. If the arguments themselves contain spaces or other characters that are special to the command line shell, then enclose those arguments in single or double quotes.

php path\to\file.php 'arg1' 'arg2' 'arg3' 'arg_n'

An example of running a PHP script with three arguments:

php C:\Users\Alex\Documents\PHP\test.php 'Alex' 'hackware.ru' 'Admin'

How to access arguments in a PHP script

The arguments passed are contained in the $argv array. Moreover, the ordinal number of the argument corresponds to the number in the array. That is, the first argument will be placed in $argv[1], the second in $argv[2], and so on.

The very first element of the array named $argv[0] contains the full path to the script to run.

The content of the test.php file:

<?php

echo 'Name: ' . $argv[1] . PHP_EOL;
echo 'Web site: ' . $argv[2] . PHP_EOL;
echo 'Status: ' . $argv[3] . PHP_EOL;

Let's run it and pass three arguments to the script:

php C:\Users\Alex\Documents\PHP\test.php 'Alex' 'hackware.ru' 'Admin'

How to get data from user in the console in PHP

Thanks to the passed arguments, the script can perform actions not only with the data written in it, but also with other values specified when the script was run.

By the way, when working in a web server environment, that is, when a PHP script performs tasks for a website, the ability to pass arguments to it is implemented using the HTTP GET and POST methods. These arguments are passed before starting the script, and after starting the PHP script, new data cannot be sent – you need to wait for the program to finish running, and, if necessary, run it again with new data.

While the script is running, it may be necessary to enter new data; in the console, this is achieved using a prompt, into which the user can enter a value and press Enter to pass it to the script. In the context of a website, there is no such possibility – to transfer data already during the execution of the script. That is, the console launch of PHP scripts with arguments is not only easier (no need to fiddle with an HTML form), but even more flexible.

PHP uses the readline function to query the user.

This feature works the same on both Windows and Linux. Moreover, on Linux, it has the interactive capabilities of Bash, for example, it saves the input history, to which you can return using the arrows. On Windows, this feature has appeared since PHP 7.1.

If you really need to, you can configure the auto-completion of the input data. All GNU Readline features are covered here. I will only touch on readline, which reads the line entered by the user. With this function, you can specify one optional argument – a string that will be shown to the user at the prompt.

An example of a PHP console script that asks the user for data at a command line prompt:

<?php

$len_min = readline("The minimum number of characters in a password is: ");
$len_max = readline("Maximum number of characters in a password: ");
$base = readline("Password base (0 - digits; 1 - lowercase letters; 2 - uppercase letters): ");

echo "Received initial data: \r\nPassword length from $len_min to $len_max characters ";
switch ($base) {
	case 0:
		echo "and the base of the password is in lowercase digits.";
		break;
	case 1:
		echo "and the base of the password is in lowercase letters.";
		break;
	case 2:
		echo "and the base of the password is in uppercase letters.";
		break;
}

How to execute PHP commands interactively

If you need it, then you can work with the PHP interpreter interactively, entering the code line by line. In this case, the code is executed after pressing the Enter button, but the values of the variables are saved within one session. That is, you can assign a value to a variable and then use it in other lines.

To run an interactive shell:

php -a

How to run individual PHP commands

Use the -r option to run individual commands:

php -r 'echo 2**100;'

Leave Your Observation

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