PHP Parameters - Command Line

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

PHP Parameters - Command Line

Post by timWebUK »

I did try searching Google, but I really was not sure exactly what to find so the results I got back were very sparce so I was hoping you guys could help as it is probably a simple issue.

I'm writing a PHP function that takes a file path as a parameter, how can I run this PHP function from the command line and pass the parameter that way?

Cheers.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP Parameters - Command Line

Post by requinix »

You can't run just functions from the command line. Only entire files.

When run from the command line, $argv and $argc correspond to the C/C++ equivalents: $argv[0] is the script being run, [1] is the first argument, [2] the second, and so on. $argc is the number of arguments - aka count($argv).
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

Re: PHP Parameters - Command Line

Post by timWebUK »

Ah ok, that makes sense! Cheers, thanks for the fast response.
katierosy
Forum Commoner
Posts: 27
Joined: Wed Apr 07, 2010 8:39 am

Re: PHP Parameters - Command Line

Post by katierosy »

Most important is space in query string as below.

in a php file say test.php these lines.

$query_string = ' id=' . '1000' .' IMG_SRC=' . 'URL';

$file = "x.php";

exec("nohup /usr/bin/php -f " . $file . $query_string . " > /tmp/mycronlog.txt &");

and in another php file namely x.php

for($i=1;$i<count($argv);$i++){
$request = split('=',$argv[$i]);
$_GET[$request[0]] = $request[1];
}

you will get $_GET['IMG_SRC'] and $_GET['id'] like this
Post Reply