Command line arguments

Command line arguments

Postby David Berthelot » Wed Feb 02, 2011 12:08 pm

PHP is mostly known for being server side language.
When you use it for scripting, keep in mind that you need to get command line arguments:

For example, if the command line is:
Code: Select all
myscript.php Hello myfile.txt


Arguments must be retrieved using $argv, so in the previous example:
Code: Select all
$argv[1] => Hello
$argv[2] => myfile.txt

for more info:
http://php.net/manual/en/reserved.variables.argv.php
User avatar
David Berthelot
 
Posts: 43
Joined: Sun Jan 16, 2011 5:06 pm

Re: Command line arguments

Postby pmachado » Sat Feb 26, 2011 11:48 am

PHP is awesome, i solved the mars explorer using it!
User avatar
pmachado
 
Posts: 15
Joined: Mon Feb 21, 2011 10:12 pm

Re: Command line arguments

Postby David Berthelot » Mon Sep 12, 2011 5:58 am

The simplest way to read a file that I know of:
Code: Select all
<?php
$data = file_get_contents($argv[1]);
?>
User avatar
David Berthelot
 
Posts: 43
Joined: Sun Jan 16, 2011 5:06 pm

Re: Command line arguments

Postby Reid Workman » Wed Sep 14, 2011 11:24 am

Even simpler =]
Code: Select all
$contents = file($argv[1]);
User avatar
Reid Workman
 
Posts: 3
Joined: Sat Jan 29, 2011 7:16 pm


Return to PHP

Who is online

Users browsing this forum: No registered users and 1 guest

cron