Developers , who uses ubuntu actually know the taste of terminal line or command line. Laravel developers also know that , laravel introduced "artisan" which runs with terminal line. It is really so amazing.
Thats why today I am writing this article. Today I am trying to show you how to use command line for php and also can build an application that can run with command line.
Before that , lets check if php is working in command line or terminal line. By default php will automatically integrated to the environment when lamp is setting up or installing php manually. In windows, wamp will install it when wamp is being installed. But it didn't work directly when I worked with xampp. Xampp has a command line shell link on its window where from we start apache, sql etc. This shell integrates php , so we can use this shell or manually set to the windows environment . To manually set php in environment , can check this link.
I don't know what the case in mac, I don't have mac or didn't used . So, mac user need to solve it by their own way.
Ok, I think everyone now can use php from command line. Now check our php from command line. Start your apache server and from command line , execute the line below -
-------------
#php -v
-------------
If you see something like below , then your system is ready.
Expected Output:
---------------------------------------------------------------------
PHP 5.6.19-1+deb.sury.org~wily+1 (cli)
Copyright (c) 1997-2016 The PHP Group ...
.................
----------------------------------------------------------------------
Ok, lets start our main part.
In command line, php can execute php code and also php file. Lets execute a php command on terminal -
-------------------------------------------------------------------
# php -r "echo 'Hello World'. PHP_EOL;"
-------------------------------------------------------------------
Output:
-------------------
Hello World
-------------------
Well, if this work with your system, then we can go next step, but if not something wrong on settings of system environment.
Let's execute a php file from command line. Create a file with name "app.php" on you server. Type some php code , in my case , I typed following code-
---------------------------------------------------------------------------
<?php
echo "This is my first command line app \n";
----------------------------------------------------------------------------
And command line commad (Open your command prompt in the folder where you keep "app.php" file , to know how to do this click here) :
------------------------
# php app.php
-----------------------
Output:
-------------------------------------------------------
This is my first command line app
------------------------------------------------------
This also working nicely. Now , we need to pass some variable to the app.php file. Before that , need to know something. Variables that passing to the file through command line will be passed as array (and it is $argv , other variable name doesn’t work in my system, I tried). So, we have to call through array to get these variable.
So, lets pass some data. Change the code of "app.php" file to the following code -
-----------------------------
<?php
var_dump($argv);
echo " \n";
----------------------------
Now from terminal , execute the following code :
-----------------------------------------------
# php app.php "Hello World"
-----------------------------------------------
Output :
------------------------------------------
array(2) {
[0]=>
string(7) "app.php"
[1]=>
string(11) "Hello World"
}
-----------------------------------------
Looks here, the first value of $argv array is "app.php" which is the name of our file and second value is "Hello World" that we want to pass to our file. Usually , we will call values from index "1", cause we already know the name of file.
Ok, now another interesting thing is, we can use our file without the extension ".php" . So, remove the ".php" extension from file name and execute the command again, it should be give same result.
Ok, I already discussed our preliminary topics that need to build the application. Let's build this application.
My application is a calculator. We can sum, subtract , multiply, divide 2 numbers using this calculator on command line. We will use oophp to do this.
So, lets change the code of "app" file to the following code :
------------------------------------------------------------------------------------------------------
<?php
class calculator
{
function __construct()
{
echo "Welcome to my app. This is a simple calculator \n";
}
public function sum($val1,$val2){
echo $val1." + ". $val2 . " = " . ($val1+$val2)."\n";
}
public function subtract($val1,$val2){
echo $val1." - ". $val2 . " = " . ($val1-$val2)."\n";
}
public function multiply($val1,$val2){
echo $val1." X ". $val2 . " = " . ($val1*$val2)."\n";
}
public function divide($val1,$val2){
echo $val1." / ". $val2 . " = " . ($val1/$val2)."\n";
}
}
$calculator= new $argv[1];
$calculator::$argv[2]($argv[3],$argv[4]);
--------------------------------------------------------------------------------------------------
Ok, Our app is complete, now run this app. Open terminal or command prompt in the folder where the "app" file is existed and run following command :
To sum 2 number(Suppose 56 and 48) :
-------------------------------------------------------
#php app calculator sum 56 48
---------------------------------------------------------
Output:
----------------------------------------------------------------------------
Welcome to my app. This is a simple calculator
56 + 48 = 104
-----------------------------------------------------------------------------
To subtract,multiply or divide replace "sum" with "subtract" , "multiply" or "divide".
Thanks for reading this article, comment your opinion and suggestion.
No comments:
Post a Comment