Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

This question already has an answer here:

I am trying to understand why am I getting this error :

PHP Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)

public function display(){

    $sql  = "SELECT * FROM `gift_cards`";

    $resultset= mysqli_query($con,$sql);
    while($rows= mysqli_fetch_assoc($resultset)){
        echo "<option value="."\"$rows['GIFT_CARD_ID']\"><h2>"."$rows['PRICE']</h2></option>";
    }
}

HTML:-

<select placeholder="GiftCards" id="a" name="a" value="">
    <?php $giftCards->display(); ?>
</select>
share|improve this question

marked as duplicate by Rizier123 php Jan 17 at 5:29

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3  
Give echo "<option value='" . $rows['GIFT_CARD_ID'] . "'><h2>" . $rows['PRICE'] . "</h2></option>"; A go :) – Matt Jan 17 at 3:35
    
Thanks Matt ! Post is as an answer and I will mark it. – Web Developer Jan 17 at 3:39
up vote 0 down vote accepted

You have mismatched quotation marks thanks to the array references. Try changing your echo to something like this:

echo "<option value=".$rows['GIFT_CARD_ID']."><h2>".$rows['PRICE']."</h2></option>";

with distinct concatenations for each variable. It's just personal preference but I prefer this for maintainability/clarity when working with complex concats. (for example, even in the StackOverflow syntax highlighting, the variables are now easily discerned instead of appearing like a big string in your IDE.)

share|improve this answer
    
Thanks Tim ! But Matt responded first in the comments, if he does not post the answer, I will mark yours. – Web Developer Jan 17 at 3:43
    
Aha, didn't see that -- he must have posted while I was typing. Glad to hear you got it figured out! – Tim Jan 17 at 3:48

If you want to use $variable['key'] syntax inside a double-quoted string, you need to put curly braces around it:

            echo "<option value="."\"{$rows['GIFT_CARD_ID']}\"><h2>"."{$rows['PRICE']}</h2></option>";
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.