2

I'm trying to pass arguments to a PHP script as I run it in CLI. I've been using the following code to do so...

// Set argument variables
// A is the filetype
// B is the filename and directory
$arguments = getopt("a:b:");

// If we're importing pies...
if($arguments['a'] == 'pies') {
    echo 'File Conversion and Import of PIES Data Beginning Now...' . "\n\n";
}

And the command would look something like this...:

php fileimport.php -a pies -b /filepath/filename.xml

I've seen arguments passed in other ways though (for other scripts) such as...

php fileimport --type=pies --path=filepath/filename.xml

Personally I prefer that method but I don't know how to code my PHP script to collect these arguments, let alone expect them. I'm sure it is ridiculously easy and I'm sorry if I've missed the obvious, but I'm hoping to get some direction here.

Thanks!

2 Answers 2

5

As described in the PHP documentation:

$shortopts  = "";

$longopts  = array(
    "type:",
    "path:",
);
// Get command line arguments
$options = getopt($shortopts, $longopts);
echo 'Input Options: ', PHP_EOL;
var_dump($options);
Sign up to request clarification or add additional context in comments.

Comments

0

try this

<?php
    $a=$argv[1];
    $b=$argv[2];

?>

and your CLI Call should be

php fileimport.php TEST1 TEST2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.