views:

47

answers:

4

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>";
+1  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!

clarkf
A: 

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>";
Hippo
+5  A: 

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>
DVK
+1 for the edit ... use Templating instead of echo!
Stephen P
+1 for the 2nd method (templating), except your syntax is a bit off. Enabling short open tags also makes templates look nicer, eg `<a href="#" onclick="jfFn('<?= htmlspecialchars($pCode) ?>'); return false;"><?= htmlspecialchars($pCode) ?></a>`
Phil Brown
@DVK re:prettified edit - Only do that if `$pCode` is purely a view-only variable
Phil Brown
@Phil - in the abscence of any indication in the Q that it isn't, i'll assume it is view-only. Good point.
DVK
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>";
Tom