manzwebdesigns icon

argv with get array sample

manzwebdesigns | PRO | 05/28/13 12:46:14 AM UTC | 0 ⭐ | 164 👁️ | Never ⏰ | []
PHP |

760 B

|

None

|

0 👍

/

0 👎

## file.php code:
<?php
$var = $_GET[ "var" ];
echo $var;
?>
 
## calling it in bash returns nothing:
[/share/Web] # php test.php var=echothis
[/share/Web] # php -f test.php var=echothis
[/share/Web] # php -f test.php -- var=echothis
[/share/Web] # php -f test.php -- ?var=echothis
[/share/Web] #
 
 
## check contents of $argv by using this code:
<?php
$count=  count($argv);
// start at index 1 to avoid parsing the script name
for($i=1; $i<$count; $i++) {
  $data = explode('=', $argv[$i]);  // explode the value into data
  $key = $data[0];
  $val = $data[1];
  echo "{$key}: {$val}\n";
}
?>
 
## ...returns this:
[/share/Web] # php test.php var=echothis
array(2) {
  [0]=>
  string(8) "test.php"
  [1]=>
  string(12) "var=echothis"
}
[/share/Web] #

Comments