Someone
Someone

Reputation: 10575

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'>$qty</span>:

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

but I am getting an error.

Upvotes: 1

Views: 873

Answers (7)

Your Common Sense
Your Common Sense

Reputation: 157828

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

Upvotes: 2

ircmaxell
ircmaxell

Reputation: 165193

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>';

Upvotes: 1

joebert
joebert

Reputation: 2663

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

Upvotes: 1

Marc B
Marc B

Reputation: 360572

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;

Upvotes: 2

Iznogood
Iznogood

Reputation: 12843

To get exacly what you asked you need:

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

Upvotes: 1

Chris
Chris

Reputation: 12078

Using single quotes:

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

Using double quotes:

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

Upvotes: 3

Quentin
Quentin

Reputation: 943108

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.

Upvotes: 1

Related Questions