Help - Search - Members - Calendar
Full Version: PHP
Hostony Board > General Support > PHP/MySQL
kanasta
can php take command line arguments when run from the shell?


I need to use it to send a file to an e-mail address. I am using it to send a backup of my database which is dumped using mysqldump . Each table is then checked against the old version to see if there were any changes, and then the ones which are changed I will pass to the php scrip to send
MartinB
Parameters are handled by PHP through the use of two predefined global variables $argc and $argv representing the number of parameters and their values as shown below:

Assuming the following command line:

CODE
$ ./myscript.php param1 param2


...And the PHP script being executed:
CODE
   <?php
            print "The total parameters count was: $argc\n";
            print "The parameters were: ";
            foreach ($argv as $val){

               print "$val ";

            }
          print "\n";
   ?>

The following output will be produced:

CODE
   $ ./myscript.php param1 param2
   The total parameters count was: 2
   The parameters were: myscript.php param1 param2


As you can see, the $argc parameter is an integer representing the total number of parameters passed on the command line. However, notice that the second variable $argv has an extra value! To assist the developer, the first value of the $argv array will be the name of the script executed followed by any command line parameters. Note that regardless of the presence of command line parameters, $argv will always contain the name of the script file being executed.

-------------------------------------------------------------

You can use a foreach:

CODE
foreach ($argv as $option)
{
 switch ($option)
 {
   case 'one':
     echo '1';
     break;
   case 'two':
     echo '2';
     break;
  }
 echo "\t";
}
kanasta
thanks. that looks good. laugh.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2024 Invision Power Services, Inc.
IPS Driver Error

IPS Driver Error

There appears to be an error with the database.
You can try to refresh the page by clicking here