php parameter usage in Unix (beginner question)

Discussion in 'Programming' started by Genisys, Apr 27, 2010.

  1. #1
    Consider this php script test.script, and the call: php test.script 4444

    <?php
    $card = $argv[ 1 ];
    echo "$card\n";
    require_once 'HTTP/Request.php';
    $req = new HTTP_Request( 'http://www.???.com?cc_number=4444&exp_date=0612' );
    $req -> sendRequest();
    ?>

    (Note that I've omitted the actual URL I'm testing with, as that isn't a factor).

    The echo command correctly displays 4444 to my screen, but my goal here is to use the passed parameter in the HTTP_Reqest line, as in:

    $req = new HTTP_Request( 'http://www.???.com?cc_number=$card&exp_date=0612' );

    But using the "$card" syntax exactly how I have it in the line above does not work, when the hardcoded "4444" above does work properly. There must be something I do not yet understand about how to use variables in php, but I cannot find syntax examples anywhere. Can anyone direct me as to how I can utilize an input parameter as a variable in the HTTP call above? Any help is much appreciated.
     
    Genisys, Apr 27, 2010 IP
  2. Kaimi

    Kaimi Peon

    Messages:
    60
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Replace single quotes with double quotes
     
    Kaimi, Apr 27, 2010 IP
  3. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #3
    Its your use of quotes; if you use doouble quotes variables will be expanded, in single they will not.

    consider this.

    <?php
    $card = 4444;
    
    print 'hello $card';
    print "hello $card";
    ?>
    PHP:
    OUTPUT:
    hello $card
    hello 4444

    You can get away with it by concatenating the value;

    $req = new HTTP_Request( 'http://www.???.com?cc_number='.$card.'&exp_date=0612' );
    PHP:
     
    lukeg32, Apr 27, 2010 IP
  4. Genisys

    Genisys Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks - your suggestions work. It's tough being a newbie :)
     
    Genisys, Apr 27, 2010 IP