0

I want to pass shell variables to a PHP script. This is my attempt:

var1=`echo name_16`
var2=`echo /home/place/end/name_16`
php script.php ${var1} ${var2}

These variables contain special characters [)(*&^%$#@!~<>_] but no hyphens.

I am running PHP 4.3.9 cgi.

I attempt to pick up the variables in my PHP script thus:

    $fish = $argv[1];
    $sticks = $argv[2];

These variables come up as null. I've tried var_dump($argv); and this returns null as well.
Am I passing these parameters correctly or am I missing something here?

6
  • From: stackoverflow.com/questions/7803025/… Try $_SERVER['argv'] Commented Feb 12, 2014 at 16:59
  • Exactly what do your parameters look like? Examples? I did a quick check with a simple alphabetic param and had no problem. Commented Feb 12, 2014 at 17:04
  • 1
    Post an actual example of how you're calling this script with the variables Commented Feb 12, 2014 at 17:05
  • the example is exactly how I am calling the script from within a .sh script. Commented Feb 12, 2014 at 17:08
  • So you're defining your variables after you call script.php? Commented Feb 12, 2014 at 17:10

2 Answers 2

1

I wanted to add this as a comment, but need the formatting ability of an answer to respond.

There's something else wrong with the code. A basic test of arguments shows that the variables should work.

Contents of foo.php:

<?php
    print "$argv[1]\n$argv[2]\n" ;

    $x = $argv[1];
    $y = $argv[2];

    print "$x\n$y\n";
?>

Then the setup:

$ var1=`echo name_16`
$ var2=`echo /home/place/end/name_16`

And execution:

$ php foo.php ${var1} ${var2}
name_16
/home/place/end/name_16
name_16
/home/place/end/name_16
$

So the problem is not reproducible using the information provided. The above is run with PHP 5.2.6 (cli) (built: May 8 2008 08:53:44), so it is, perhaps, a difference in version behavior.

Sign up to request clarification or add additional context in comments.

5 Comments

i figured it would be the version. possibly that I don't have access to cli.
@eddiecubed could you try the same simple test that I did above and see what happens, just to be sure we're apples-to-apples?
I get nulls for output. The problem here is the version. The variable register_argc_argv is set to false. Which means the argv never gets built. So i need to look into using a different way of sending these variables over.
@eddiecubed thanks for trying the specific experiment; I agree with your conclusion.
thanks for being helpful. I decided to just go with writing the variables to a text file in sh and then read the file within php. This worked.
0

just use $argv, its an array of arguments and quotes around special args.

http://www.php.net/manual/en/reserved.variables.argv.php

test.php:

<?php
  print_r($argv);
?>


>HHH="&^%$#@"
> php test.php hello args $HHH

output:

Array
(
[0] => test.php
[1] => hello
[2] => args
[3] => &^%$#@

)

3 Comments

I've tried this and it comes up with nothing. I've tried var_dump and I come up with null.
i just ran it and it worked. did you put quotes around the special args?
I've tried your test verbatim and I get empty output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.