9

I want to pass two different arguments to my script and based on the sent argument, I need my script does something.But I don't know how to define my conditional statement.

To be more precise, I want my script does searching when I pass "search" argument and alternatively showing the result when I pass "show" argument.

Here is my code:

if ($argc > 1) {
  if ($argv[0] == 'show') {
    for ($i = 0; $i <= $argv[2]; $i++) {
      //do something
    }
  }
  elseif($argv[0] == 'search') {
    //do something
  }
} else {
  echo "no argument passed\n";
}

The "IF" statement is not checking my passing argument whether it is "search" or "show"

11
  • try to use else if. Apr 14, 2013 at 11:39
  • That also does not work. Apr 14, 2013 at 11:40
  • 2
    Isn't $argv[0] the script name? Apr 14, 2013 at 11:41
  • how you execute this program in command line ? Apr 14, 2013 at 11:41
  • 1
    aydin-hassan is right, $argv[0] contains the name of script. You should check $argv[1] for first argument value.
    – zavg
    Apr 14, 2013 at 11:45

5 Answers 5

6

$argv[0] is the name of the script, that's why your code doesn't work.

If I have a file script.php:

<?php
if ($argc > 1) {
  if ($argv[1] == 'show') {
    for ($i = 0; $i <= $argv[2]; $i++) {
      print "show passed\n";
    }
  }
  elseif($argv[1] == 'search') {
    print "search passed";
  }
} else {
  echo "no argument passed\n";
}

Testing gives:

$php script.php

no argument passed

$php script.php search

search passed

$php script.php show 2

show passed
show passed
show passed
0
4

Why not?

$aPassedOptions = getopt("", ['show', 'search']);
$bShow = isset($aPassedOptions['show']);
$bSearch = isset($aPassedOptions['search']);
var_dump($aPassedOptions, $bShow, $bSearch);


script.php --show
script.php --search
script.php --show --search

more @ http://www.php.net/getopt (for example, paramter is required etc)

3

I'd use switch/case instead and your condition ($argc) is wrong as you you need action name and argument and $argv[0] is script name, so your command is argv[1] and its arguments argv[2] and up. I'd also get argc/argv from $_SERVER global

if ($_SERVER['argc'] >= 3) {
  switch( $_SERVER['argv'][1] ) {
     case 'show':
        ...
        break;

     case 'search':
        ...
        break;
   }
} else {
   // no args case
}

On general, it's usually useful to try i.e.

var_dump($_SERVER);
2

Try $argv[1] instead

http://php.net/manual/en/reserved.variables.argv.php

$argv[0] contains the name of the script which was called

1

Use $argv[1].

In UNIX model (like...C), the traditional ARGV array starts with the first binary invoked, then passes along the arguments. In PHP case, if you call your script through PHP ("php script.php") instead of hash-banging it ("./script.php"), php automatically removes itself from $argv[0].

So in your case:

$ cat argv.php
#!/usr/bin/php
<?php
    print_r($argv);
?>
$ php argv.php search
Array
(
    [0] => argv.php
    [1] => search
)
$ ./argv.php search
Array
(
    [0] => ./argv.php
    [1] => search
)
$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.