开发者

How do i enclose the Single Quotes within double quotes for php variables to be interpreted

$URN  = 1
$col2 = ABC
$qty  = 10

the above 3 values needs to be put in span tag such as <span id ='$URN:$col2'>开发者_如何学Go;$qty</span>:

$row['col1']  =  "<span id = '".$urn."'>".$qty."</span>";

but I am getting an error.


Using single quotes:

 $row['col1']  =  "<span id = '".$URN.":".$col2."'>".$qty."</span>";

Using double quotes:

   $row['col1']  =  "<span id = \"".$URN.":".$col2."\">".$qty."</span>";


You've got a few choices.

a. String concatenation with single quotes

$row['col1'] = '<span id="' . $urn . '">' . $qty . '</span>

b. Double quotes and escapes:

$row['col1'] = "<span id=\"{$urn}\">{$qty}</span>";

c. HEREDOC

$row['col1'] = <<<EOL
<span id="{$urn}">{$qty}</span>
EOL;


Variable names are case sensitive in PHP.
If you're getting "Undefined variable" error - well, this is it.

$row['col1']  =  "<span id = '$URN:$col2'>$qty</span>";


I can't see any syntax errors, but the following would be a lot more readable:

$row['col1']  =  "<span id='$urn'>$qty</span>";

It might help if you were more specific about the mysterious error you are getting.


To get exacly what you asked you need:

$row['col1']  =  "<span id = '".$urn.":".$col2."'>".$qty."</span>";


I avoid needing to think about this problem by using the sprintf function when I want to generate HTML.


Please escape your output (otherwise you're just opening the door for possible XSS attacks:

(Assuming UTF-8 Character set):

$row['col1']  =  '<span id="'.
                    htmlspecialchars($urn, ENT_COMPAT, 'UTF-8').
                 '">'.
                    htmlspecialchars($qty, ENT_COMPAT, 'UTF-8').
                 '</span>';
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜