Solved

PHP windows cmd line variable

Posted on 2006-11-24
4
724 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

803 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