Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use parameters in commanline PHP

Status
Not open for further replies.

lhg1

IS-IT--Management
Mar 29, 2005
134
DK
Hi

I have a PHP script that I want to use from the commandline, and it has a parameter.

Usally I use this in a browser

where I in the script has this.
Code:
if(!isset($_GET['tid'])) {
        $tid = "1";
} else {
        $tid = $_GET['tid'];
}

I can run it on the command line without the parametes like this.
php -c /etc/php5/apache2/ ./script.php

But can anybody tell me how to get the parameter "tid" on the one from the commandline?

Regards
Lars
 
PHP uses the argc and argv arrays like C when operated from the command line. See this link for an example. Specifically look at the section titled: diego dot rodrigues at poli dot usp dot br 02-May-2005 08:29
 
Hi

Either process the [tt]$argv[/tt] array yourself or pass it through [tt]getopt()[/tt] :
Code:
#!/usr/bin/php
[teal]<?php[/teal]

[b]echo[/b] [green][i]'Raw parameters : '[/i][/green][teal];[/teal]
[COLOR=darkgoldenrod]print_r[/color][teal]([/teal][navy]$argv[/navy][teal]);[/teal]

[navy]$opt[/navy][teal]=[/teal][COLOR=darkgoldenrod]getopt[/color][teal]([/teal][b]null[/b][teal],[/teal][b]array[/b][teal]([/teal][green][i]'tid:'[/i][/green][teal]));[/teal]

[b]echo[/b] [green][i]'Processed parameters : '[/i][/green][teal];[/teal]
[COLOR=darkgoldenrod]print_r[/color][teal]([/teal][navy]$opt[/navy][teal]);[/teal]
Code:
[blue]master #[/blue] ./lhg1.php --tid=foo bar
Raw parameters : Array
(
    [0] => ./lhg1.php
    [1] => --tid=foo
    [2] => bar
)
Processed parameters : Array
(
    [tid] => foo
)

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top