Solved

PHP windows cmd line variable

Posted on 2006-11-24
4
725 Views
Last Modified: 2012-05-05
Hi

Can somebody show me how to make a php script that I can run from the windows command line and pass a variable to it.

A simple example is a script that echo's whatever variable was passed to it from the command line

Thanks
0
Comment
Question by:zattz
  • 2
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
joachim.claeys@teleatlas.com earned 500 total points
ID: 18010630

http://us3.php.net/features.commandline

All the info you need ...

Simple example ( script.php ):

//////////////////////////////////////////////////
<?php
if ($argv)
   for ($i=1;$i<count($argv);$i++)
   {
       echo $argv[$i]);
    }
?>
//////////////////////////////////////////////////

Now execute: "@C:\php\php.exe script.php 1a 2b 3c 4d"

Hope this helps
0
 

Author Comment

by:zattz
ID: 18010651
C:\php>php.exe script.php 1a 2b 3c 4d
PHP Parse error:  parse error, unexpected ')', expecting ',' or ';' in C:\php\sc
ript.php on line 5
X-Powered-By: PHP/4.4.4
Content-type: text/html

I removed the ")" on line 5 and I get

C:\php>php.exe script.php 1a 2b 3c 4d
PHP Notice:  Undefined variable:  argv in C:\php\script.php on line 2
X-Powered-By: PHP/4.4.4
Content-type: text/html
0
 
LVL 3

Expert Comment

by:joachim.claeys@teleatlas.com
ID: 18010869
Sorry about the typing error.

Make sure you read the introduction of the referenced php link "http://us3.php.net/features.commandline"

Depending on your PHP version / build, it's possible that you need to change php.ini settings to be able to access the argv array.

(  e.g register_argc_argv TRUE )



0
 

Author Comment

by:zattz
ID: 18010882
I do apologise, I totally skipped over the link.

It's working now that I edited my php.ini to say register_argc_argv = on

Thanks:)
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question