Introduction:
PHP is a great language to develop web applications rapidly and easily. PHP code can also be run as a command line script just like C, C++, java, and so on.
PHP Package:
When we unpack the content of the PHP zip file, downloaded for the PHP website, into a directory for example C:\php, a list of modules and executables required for PHP scripts to run successfully are shown as in Diagram 1.
The following are the list of distribution:
go-pear.bat – the PEAR setup script.
php-cgi.exe – CGI executable that can be used when running PHP on IIS via CGI or FastCGI.
php-win.exe – the PHP executable for executing PHP scripts without using a command line window (for example PHP applications that use Windows GUI).
php.exe – the PHP executable for executing PHP scripts within a command line interface (CLI).
php5apache2_2.dll – Apache 2.2.X module.
php5apache2_2_filter.dll – Apache 2.2.X filter.
To run PHP scripts at the command prompt php.exe is what we are interested in. This is the executable for running PHP scripts / programs at the command prompt in a command window.
Diagram 1
Before you attempt to execute any PHP program, ensure that the path php.exe (i.e. the PHP runtime) installed on your computer has been registered in the system settings, PATH variable of your computer. The details on How to set the path to the PHP runtime in Windows XP is explained in tutorial PHP / MySQL – at the Command Prompt .
Process:
The following is an example of running PHP code at the command prompt.
Open a Notepad and then type in the PHP program as shown in Diagram 2.
Diagram 2
Click on File -> Save As.. as shown in Diagram 3.
Diagram 3
A ‘Save As’ window pops up as shown in Diagram 4.
Next, select the specific location for saving the file.
Enter the name of the file in the textbox of ‘File name:’ and then save the file by clicking on theSave button on the right hand side.
NOTE: The extension of the file should be in ‘.php’.
In the following example the ‘filename: helloword.php’ is saved in E:\ drive.
Diagram 4
The php file will be created as shown in Diagram 5.
Diagram 5
Next, to run this file on the command prompt, go to start -> Run as shown in Diagram 6.
Diagram 6
A window pops up.
Next, in ‘Open:’ , type in cmd as shown in Diagram 7.
Click on OK as shown in Diagram 7.
Diagram 7
A DOS command window opens.
Next, type in the path of the file or the drive in which your file is saved and press Enter as shown in Diagram 8.
For example:
<system prompt> e: [Enter]
You will notice that the prompt has now changed. This indicates that you are now in E:\ drive.
Diagram 8
To take a look at the files in this directory at the < System Prompt > enter dir and press Enter.
<system prompt> dir [Enter]
A list of files is displayed as shown in Diagram 9.
NOTE: The dir command displays the files in the current directory.
Diagram 9
To run helloworld.php at the command prompt, enter php followed by the php filename.ext that needs to be run as shown in Diagram 10.
<system prompt> php helloword.php [Enter]
Diagram 10
The output is displayed as shown in Diagram 11.
Diagram 11