How to pass variable from command line in PHP

How to pass a variable from the command line in PHP

Hy Folks, Today we will learn how can you pass variable in PHP using the Command line. Now you are thinking about it that Is it possible? yes, it is. Normally you pass the variable in the URL using ?ab=test and get the key value in the code which looks like this.


$_GET['ab']  // get the key
echo $_GET['ab']   // value will be print test
ab is key and the test is value 

 

If you want to run your script by command line (terminal) and you don't want to pass variable every time in the URL then hit enter for showing the result according to your script. Don't worry today we will learn how can we do this by command line. so let's Start. 


Please do follow the steps.

Step 1-  First open your php.ini file and search this text register_argc_arg. if this variable value false so just set true this variable and save the file.


register_argc_arg = false // by default
after change 
register_argc_arg = true 


Step 2 -  Now open your terminal or command line and go to the folder which files you want to run.


    

      MacBook-AirUser:script_folder User$ php php_script.php

 

 

Here php is indicating that we are running a PHP file so we write php at the beginning and php_script.php is a file name where our script in that file. if you will run only this file without passing the variable, so the result will come without that result which you do not want because you haven't passed a variable, I am telling you that condition where we want to only get the data if someone passes the variable.

lets an example you want to print hello "your name". but the condition is that if someone passes a variable by the URL or command line then it should be print Hello with your name and if you don't pass the variable then it should be print No data found.


Let's do this 

  

    MacBook-AirUser:script_folder User$ php php_script.php Folks



here I passed Folks as a variable and now we will write code in our php_script.php file that how to get this variable if someone passes by the command line.


<?php  // php_script.php file

  if(isset($argc)){  // check number of argument passed or not

       foreach($argv as $key=>$value){

            echo "Hello ".$value; 

       }

  }else{

    echo "No Data Found";

  }

?>

 

OutPut - 


 Hello Folks


I have passed only one variable you can pass multiple variables according to your requirements. if you pass multiple just a little change in the code. add <br> tag inside foreach loop so that data will print in the next line.


 

     MacBook-AirUser:script_folder User$ php php_script.php Zack Mark Johan



 Code : 


<?php  // php_script.php file

  if(isset($argc)){  // check number of argument passed or not

       foreach($argv as $key=>$value){

            echo "Hello ".$value ."<br>"; 

       }

  }else{

    echo "No Data Found";

  }

?> 
  
OutPut - 

 Hello Zack
 Hello Mark
 Hello Jhon

  1. *NotePlease don't pass variables by command line like this.
  2. Ex -  php  php_script.php?arg= Mark&arg1= Zack&arg2= Ricky


Need to know 

     know about little bit  $argc and $argv .

       

$argc -  Contain the number of arguments passed by you from the command line.
   
$argv - Contain an array of all arguments passed by you from the command line. 



Post a Comment

Please do not enter any spam link in the comment section.

Previous Post Next Post