I am running newest version of WAMPServer on Windows 7 Home Premium. It accesses database (remote) fine, it runs basic PHP fine. My issue is with variable variables. On my godaddy server, the exact code runs fine.

When I try to include a file that uses variable variables (in this case $state{$i}) in WAMPServer says:

Notice: Undefined variable: state in E:\wamp\www\CCRN411\inc\state_drop_down.inc.php on line 2.

Note that it is looking for state, not state1 as the code generates for $i= 1 which I declare on the first line of the same file.

<select class="dropdownState" name="<?php $i=1; echo 'state' . $i; ?>" id="<?php echo 'state' . $i; ?>">
<option value="DL" <?php if ($state{$i} == "DL") {echo ('selected="selected"');} ?>>DELETE</option>
<option value="ZZ" <?php if ($state{$i} == "ZZ") {echo ('selected="selected"');} ?>>NONE</option>

If I add

$state1 = . and it is empty. What is the issue with my variable variable?

Help please.

有帮助吗?

解决方案

Wrong syntax. You want ${"state$i"}, as in

if (${"state$i"} == "DL") ...
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top