How to pass variables to PHP CLI shell script

Earlier today I made a post about running PHP CLI shell script. You can read it right here.

Well now, what about if I want to pass variables? Well it’s easy. The piece of code below catches the arguments and you can make them become variables to be passed to your program.

Let’s execute this command on the shell, ./myscript.cli.php –verbose –username mardix –password mypass –action new_entry

Now the code to get the variables from the command line, and assign them to $_GET to be accessible on the page. It’s best to put this code in the beginning of the file.

        $argv = $_SERVER['argv'];

        $totalArgv = count($argv);
	if( $totalArgv > 1 ){

		for( $x = 1; $x < $totalArgv; $x++ )
		{
			switch($argv[$x])
			{
				case '--verbose':
					$Verbose = true;
				break;
				case '--username':
					$_GET[username] = trim($argv[$x+1]);
				break;

				case '--password':
					$_GET[password] = trim($argv[$x+1]);
				break;

				case '--action':
					$_GET[action] = trim($argv[$x+1]);
				break;
			}
		}
	}

	if( $Verbose ) echo "Starting script...\n\n";

	// Continue with your script below

Now all the arguments will be assigned to _GET, as we would be getting them from the URI. Also, this code must be place in the beginning of your file, just the variables can be set as soon as the program starts.

Best practice, IMHO.

When I’m making my apps and want them to be accessed via a browser and CLI, I create two files: myapp.php and myapp.cli.php. Notice that the second name ended with .cli.php. It’s to tell me that this file can be executed via command line and it won’t be executed in a anything but the CLI.

The regular .php is the real program, where .cli.php only includes myapp.php and passes the variables of the CLI to php.

To block access of your .cli.php file to browsers add the following code in the beginning of the .cli.php script:

  • if( php_sapi_name() != ‘cli’ ) die(’Access denied.’);

Now that’s how I make my APP to be executed on the CLI and web.

myapp.php : Can be executed on web browser. It’s the complete file

<?
echo("Welcome ".$_GET[username]);
// more of the script
?>

myapp.cli.php : To be executed in the command line. Also, this part is required #!/usr/bin/php -q if you want to execute it like this ./myapp.cli.php

#!/usr/bin/php -q
<?

if( php_sapi_name() != ‘cli’ ) die(’Access denied.’);

        $argv = $_SERVER['argv'];

        $totalArgv = count($argv);
	if( $totalArgv > 1 ){

		for( $x = 1; $x < $totalArgv; $x++ )
		{
			switch($argv[$x])
			{
				case '--verbose':
					$Verbose = true;
				break;
				case '--username':
					$_GET[username] = trim($argv[$x+1]);
				break;

				case '--password':
					$_GET[password] = trim($argv[$x+1]);
				break;

				case '--action':
					$_GET[action] = trim($argv[$x+1]);
				break;
			}
		}
	}

	if( $Verbose ) echo "Starting script...\n\n";

include_once("./myapp.php");


?>

There we go, that’s it. You can leave me your comments and say what’s up!

Leave a comment