开发者

using php to generate html and javascript - quote/doublequotes

开发者 https://www.devze.com 2023-01-21 22:19 出处:网络
This php statement works, but is there a more readable way to express it, in terms of quotation marks.

This php statement works, but is there a more readable way to express it, in terms of quotation marks.

$pCode = $row['SIZIP'];
echo " <a href='#' onClick='jfFn(\"".$pCode."\");return false;' > " . $pCode . "</a&开发者_开发百科gt;";


You could use heredoc notation:

$pCode = $row['SIZIP'];
echo <<<EOD
<a href='#' onClick='jfFn("$pCode");return false;' > $pCode</a>
EOD;

Also, consider using template stuff for what it's meant to be used insetad of using echo to print HTML? You might as well have been using bash otherwise :)

<!-- your main document (non-PHP code) is here -->
<?php $pCode = $row['SIZIP'];  ?>
<a href='#' onClick='jfFn("<?php echo '$pCode';?>");return false;' >
<?php echo '$pCode';?></a>

or as per Phil Brown's comment

<?php $pCode = $row['SIZIP'];  ?>
<a href="#" onclick="jfFn('<?= htmlspecialchars($pCode) ?>'); 
                     return false;"><?= htmlspecialchars($pCode) ?></a>

I assume this can be prettified a bit by:

<?php $pCode = htmlspecialchars($row['SIZIP']);  ?> 
<a href="#" onclick="jfFn('<?= $pCode ?>'); return false;"><?= $pCode ?></a>


You could use printf()?

$pCode = $row['SIZIP'];
printf("<a href='#' onClick='jfFn(\"%s\");return false;'>%s</a>",$pCode);

It's a little more readable now!


This should work

$pCode = $row['SIZIP'];
echo " <a href='#' onClick='jfFn(\"$pCode\");return false;' >$pCode</a>";

or this:

echo " <a href='#' onClick='jfFn(\"{$row['SIZIP']}\");return false;' >{$row['SIZIP']}</a>";


You can use { and } when echo'ing with double quotes (only!), like:

$pCode = $row['SIZIP'];  
echo "<a href='#' onClick='jfFn(\"{$pCode}\"); return false;'>{$pCode}</a>";
0

精彩评论

暂无评论...
验证码 换一张
取 消