Mubashir Anwar
Mubashir Anwar

Reputation: 15

How to escape double quotation marks in php? /" is not working

I have spent hours trying to get this code to work:

<?php foreach ($stocks as $stock):?>
    <option value="<?= $stock(\"symbol\") ?>" > <?= $stock("symbol") ?></option>   
<?php endforeach ?>

When running this code I get an error:

Parse error: syntax error, unexpected '"symbol\") ?>"' (T_CONSTANT_ENCAPSED_STRING), expecting identifier (T_STRING) in /home/jharvard/vhosts/pset7/templates/sell_form.php on line 7

Please help!

Upvotes: 0

Views: 980

Answers (5)

Vernard Luz
Vernard Luz

Reputation: 291

I'm not sure what you're trying to do, but since you used a foreach, I'm guessing that this is a multi-dimensional array.

Try this:

<?php
$stocks = array(
    array(
        "symbol" => "value2"
        ),
    array(
        "symbol" => "value2"
        )
    );
?>
<select>
<?php foreach ($stocks as $stock):?>
    <option value="<?= $stock["symbol"] ?>"> <?= $stock["symbol"] ?></option>   
<?php endforeach ?>
</select>

Edit: If you want the display to have a double-quotes, use this:

    <option value="<?= $stock["symbol"] ?>">"<?= $stock["symbol"] ?>"</option>   

Upvotes: 1

Mastacheata
Mastacheata

Reputation: 2043

There's no need to escape the quotation marks at all here. \"symbol\" is treated like a constant here, but there is no constant with that name.

The surrounding HTML quotation marks are only evaluated by the Browser and play no role to the PHP interpreter. PHP will only evaluate the stuff inside and ignore whatever is surrounding and the HTML parsing of the browser will never see the quotation marks in your PHP code, but only the resulting string of $stock("symbol")

Upvotes: 1

codeGig
codeGig

Reputation: 1064

<?php foreach ($stocks as $stock):?>
<option value="<?= $stock("\"symbol\"") ?>" > <?= $stock("symbol") ?></option>   
<?php endforeach ?>

you can use single quote or you use like above code.

Upvotes: 0

user399666
user399666

Reputation: 19879

Change it to:

<option value="<?= $stock("symbol") ?>" > <?= $stock("symbol") ?></option>

You were escaping quotes that didn't need to be escaped.

Upvotes: 3

Oliver Spryn
Oliver Spryn

Reputation: 17348

Try this, just changing your quotes:

<?php foreach ($stocks as $stock):?>
   <option value="<?= $stock('symbol'); ?>" > <?= $stock("symbol"); ?></option>   
<?php endforeach ?>

Note: untested suggestion.

Upvotes: 2

Related Questions