View Full Version : Undefined variable

02-28-2012, 03:42 AM
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.

echo "<td bgcolor='00ffcc'>${$temp}</td></tr>";

Here's the full thing.


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


02-28-2012, 03:52 AM
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>";

02-28-2012, 04:59 AM
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.

02-28-2012, 07:05 AM
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.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum