Solved

PHP windows cmd line variable

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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.  …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now