Parse error: syntax error, unexpected T_ENCAPSED_A

2019-03-02 05:41发布

问题:

I might have a syntax error or something but I don't see nothing.

<select id="cd" name="cd">
    <?php
    while($row=mysql_fetch_array($cdresult)) { 
        echo "('<option value='$row['Poblacion']'></option >'.'<br />)"; 
    } 
    mysql_close($link); 
    ?>
</select>

On the echo line, I have the error :

[error] [client] PHP Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /var/www/slimtest/views/nuevo.php on line 89

Maybe you can help, because I don't see the error D"=

回答1:

This should work:

<select id="cd" name="cd">
    <?php
    while($row=mysql_fetch_array($cdresult)) { 
        echo "<option value=".$row['Poblacion']."></option><br/>"; 
    } 
    mysql_close($link); 
    ?>
</select>


回答2:

When using array variables inside of strings it's usually better to use the complex syntax:

echo "('<option value='{$row['Poblacion']}'></option >'.'<br />)"; 

Alternatively you can remove the quotes in the array key:

echo "('<option value='$row[Poblacion]'></option >'.'<br />)";

PHP String Variable Parsing



回答3:

Try changing echo line to this:

echo '<option value="' . $row['Poblacion'] . '"></option >'; 


回答4:

This line is a mess

echo "('<option value='$row['Poblacion']'></option >'.'<br />)"; 

First off, you can't use other characters around an <option> tag (the <br> tag is meaningless there). And then you leave the text of the tag blank. Finally, you're using double quotes around the whole thing, leaving PHP to try and interpret it. My bet is you're trying to do this instead.

echo '<option value="' . $row['Poblacion'] . '">' . $row['Poblacion'] . '</option>';

This will generate a proper tag AND populate it with the text of your field as well (so users can see what they're selecting). The way you had it, even if it were proper HTML, you'd have a dropdown of nothing but blank entries.