How to Run php Codes in Commandline

In this tutorial i am going to show you how to run your php scripts in windows or Linux commandline. First of All, You need to install php5-cli extension. Use phpinfo() function to check installed php extensions.

In Linux System, you can use following Command to install php5-cli.
sudo apt-get install php5-cli
 If you already installed php5-cli you are eligible to run php codes in commandline.

open a notepad or your favorite Text Editing tool.Copy and pasete below php code snippet inside your notepad. Save it with .php extension.
<?php
echo "Hello Word !\n";
echo "php Commandline Tutorial\n";
?>

open command prompt.


For Windows Users
Go to php installed directory using cd Command. If you don't want to go to php installed directory, add php installed path to path variable in your system. Then you can issue php commands anywhere in your system without going to php installed directory.

I have installed php in xampp\php directory So I have added following line to my path variable. Now I can issue php commands from anywhere in the system.

C:\xampp\php

For Linux Users
You Do not want to add php installed directory to path variable. because php command is already there in your system.


OK !
Type Following Command in your Command prompt to run your php script.
php -f  your_php_script.php

After that you can see the following output.
php Commandline output
You can write more complex php Codes and run these script in commandline.
Cheers !

0 comments:

Post a Comment

Ask anything about this Tutorial.