简体   繁体   中英

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!

Change it to:

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

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

Try this, just changing your quotes:

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

Note: untested suggestion.

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")

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>   
<?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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM