Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Oct 2010
    Posts
    128
    Thanks
    26
    Thanked 0 Times in 0 Posts

    Undefined variable

    Full Disclosure: This is for a class. However, the problem I'm having isn't part of the homework. It's an error from the book. I've typed it and even used the same source file that came with the book, but it won't work

    I was a form where people select Vacation Spots. The PHP displays, in a table, the vaction cities they selected. If they select all of the options, it works fine. If they don't, then "Undefined Variable" shows us on the next page.

    If it's not selected, the value should be null. BUt it's not working and I'm not seeing anywhere in the book, well this chapter anyway, that shows what to do.

    Here's the line of code that the browser shows is the problem.

    PHP Code:
        echo "<td bgcolor='00ffcc'>${$temp}</td></tr>"
    Here's the full thing.
    PHP Code:
    <?php

      extract
    ($_REQUEST);
      for( 
    $i=1$i <= 6$i++){
        
    $temp "place$i";
        echo 
    "<tr><td bgcolor='00ff99'>$temp</td>";
        echo 
    "<td bgcolor='00ffcc'>${$temp}</td></tr>";
      }

    ?>

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,538
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    Looks like they're trying to do "variable of a variable" ...

    What happens if you change this line:
    echo "<td bgcolor='00ffcc'>${$temp}</td></tr>";

    To this:
    echo "<td bgcolor='00ffcc'>".$$temp."</td></tr>";

  • #3
    Regular Coder
    Join Date
    Oct 2010
    Posts
    128
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Nope didn't work. But the book does say that line is a variable variable.

    Blah. The homework is simple. Add another location to the form. But, like I said, even before I changed the code, the same error was coming up.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Like register globals, extract is also a poor solution. The problem is simply these groups of $place{x} just don't exist. Verify that the html or cookies contain the proper name conventions and not something like an underscore separating them or an array.
    Also, I'd use ${$var} in the case of variable variables, although in all actuality you never need to use variable variables which are ultimately more confusing than temporary variables will be.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •