Problem with a PHP script.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Roger

    Problem with a PHP script.

    I would be grateful for some help with this code.
    The comments in the code show what I'm expecting as output, but
    instead I'm getting "undefined variable peru" and "undefined variable
    japan"...

    <?
    $peru = array("domain"= >"pe", "capital"=>"Lim a");
    $japan = array("domain"= >"jp", "capital"=>"Tok yo");

    function show ( $country ) {
    print "Internet domain = ".${$countr y}["domain"]."\n";
    print "Capital city = ".${$countr y}["capital"]."\n";
    }

    show ("peru");
    // Prints:
    // Internet domain = pe
    // Capital city = Lima

    show ("japan");
    // Prints:
    // Internet domain = jp
    // Capital city = Tokyo
    ?>

    I'm new to PHP- as you can tell!
    Thanks in advance.

  • Jeff Rodriguez

    #2
    Re: Problem with a PHP script.

    Roger wrote:
    [color=blue]
    > function show ( $country ) {
    > print "Internet domain = ".${$countr y}["domain"]."\n";
    > print "Capital city = ".${$countr y}["capital"]."\n";
    > }[/color]

    function show ( $country ) {
    print "Internet domain = " . $country["domain"] . "\n";
    print "Capital city = " . $country["capital"] . "\n";
    }

    or more simply

    function show ( $country ) {
    print "Internet domain = {$country["domain"]}\n";
    print "Capital city = {$country["capital"]}\n";
    }

    or even a heredoc

    function show ( $country ) {
    print <<<EOF
    Internet domain = {$country["domain"]}
    Capital city = {$country["capital"]}
    EOF;
    }

    Comment

    • Tim Van Wassenhove

      #3
      Re: Problem with a PHP script.

      In article <5qoaa0dj41igtb 9p9n6vpu6kff9in ucvjr@4ax.com>, Roger wrote:[color=blue]
      > function show ( $country ) {
      > print "Internet domain = ".${$countr y}["domain"]."\n";
      > print "Capital city = ".${$countr y}["capital"]."\n";
      > }[/color]

      [color=blue]
      > show ("peru");
      > show ("japan");[/color]

      You pass a "string" to a function that expects an array.

      show($peru);

      --

      Comment

      Working...