Take the 2-minute tour ×

I am not sure if this is possible, but I need to make a dirty hack. Assume I am invoking php from cli

php script.php one two

then in script.php I am doing something with two parameter, and then I would like to remove it, so the third party code that is included later on thought, that the initial command was

php script.php one

I tried unset($argv[2]) and unset($GLOBALS['argv'][2]) but this does not work. Is it after all possible what I am trying to do ?

UPDATE: Does not work = I get exception from third party code "Too many arguments"

UPDATE: of course it should be argv[1], but it is a typo only in this question, not in the actual problem. Please assume I have written argv[1] :)

share|improve this question
What does 'does not work' mean? –  arkascha Sep 28 '12 at 10:35
It does seem to be working for me, unset() -ing or array_pop() both. –  complex857 Sep 28 '12 at 10:36
add comment

4 Answers

up vote 2 down vote accepted

Manipulating $argc and $argv from within a php script is not possible AFAIK. This is because the values are not part of the script itself, but defined in the calling scope.

As an alternative you could copy the array, pop the last argument and call the "third party code" by using exec() or system() and handing over the reduced copy.

share|improve this answer
add comment

You're trying to remove the third element with unset($argv[2]).

Array are zero-based. So to remove the second element use:


share|improve this answer
hehe that's right, I made a typo in the question, but not in the actual problem. I have to pass few parameters in a real code and I am sure I did not make off-by-one mistake in the real case :) was using var dump for debugging, ensuring it disappeared from array, etc. –  mkk Dec 5 '12 at 11:56
add comment

You can change the values of the variables:

  • $argv
  • $argc
  • $_SERVER['argv']
  • $_SERVER['argc']
  • $GLOBALS['argv']
  • $GLOBALS['argc']

This may influence code written in PHP that relies on this variables. (And this might be already what you want).

I found no way to change argv and argc under the hood in a way that the behaviour of PHP internal functions like getopt() can be influenced.

share|improve this answer
add comment

Yes you could ofc alter this if you wish.

I use this code for a cli script I'm running

foreach( $argv as $key => $arg )
  if( $arg == '-s' )
    $s = $argv[ ++$key ];
    unset( $argv[   $key ], $_SERVER[ 'argv' ][ $key ] );
    unset( $argv[ --$key ], $_SERVER[ 'argv' ][ $key ] );
    $argv = array_values( $argv );
    $_SERVER[ 'argc' ]--;
share|improve this answer
add comment

Your Answer


