简体   繁体   中英

Wrap php variable in quotes before echoing

I need to use a php variable to echo the id into an input field:

<input type="text" id={{ strtolower($val) . "Field" }}/>

But with the code above the output is

<input type="text" id=waterField/>

I want the output to wrapped in double quotes like:

<input type="text" id="waterField" />

I am using laravel but am totally willing to accomplish it without. Is there a simple way to accomplish this? Thank you.

use

echo '<input type="text" id="'.strtolower(htmlspecialchars($val)). 'Field"/>';

OR idle way to use html and php is

<input type="text" id="<?php echo strtolower(htmlspecialchars($val)); ?>Field"/>

If $val can have quotes in it, then need to clear them by using htmlspecialchars .

考虑到您已启用短标签,这就是纯PHP。

<input type="text" id="<? echo strtolower($val) ?>Field" />

为什么不只是:

<input type="text" id="{{ strtolower($val) }}Field"/>

You can simply add the double quotes into your html text. Escape the double quotes like this \\" so php won't parse them and you're fine.

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