Solved

PHP windows cmd line variable

Posted on 2006-11-24
4
727 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

756 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