Jump to content

echo single quotes within double quotes


pwired
 Share

Recommended Posts

I have this html line:

<div id="button2" class="button_style" onclick="location.href='page2.html';" style="cursor: pointer;">
product
</div>

This html has double quotes and single quotes and is no problem when this line is hard coded.

But I want to echo this line and then there is a problem because there are already single quotes

in that line in the part onclick="location.href='page2.html';"

echo always starts and ends with a double quote like this:

echo " html ";

If I want to echo the above html line it would start with double quotes and have to replace

the next ones with single quotes like this:

echo "<div id='button2' class='button_style' onclick='location.href='page2.html';' style='cursor: pointer;'>
product
</div>";

But what about the part onclick="location.href='page2.html';"

I can replace the double quotes with single quotes

but what about the single quotes in the part href='page2.html'

How am I going to replace those single quotes ?

Edit:

I think I found something on stack overflow

http://stackoverflow.com/questions/3383838/how-to-use-single-quotes-inside-single-quotes

Link to comment
Share on other sites

Yes lots to study  ^_^ :

a) double quotes forces PHP to evaluate the string (even though it might not be needed)
b) a string between single quotes is not evaluated.
c) parsing a variable between strings takes more memory than concatenate a variable.
 

So this should work:

echo '<div id="button2" class="button_style" onclick="location.href=\'page2.html\';" style="cursor: pointer;">product</div>';

Edit:

Tried it and the above works, even the onclick part :)

Tried also the other way around = starting with  echo "<div id= . . . and escaping with \'page2.html\'

obviously the button appeared but the onclick part failed.

  • Like 1
Link to comment
Share on other sites

Just my two cents: When using HTML in (that is, within a function), I tend to use heredocs, which makes code visualisation easier - especially when you're using an editor like Sublime Text, or something along those lines.

In your case, you could do the following:

print <<<HTML
    <div id="button2" class="button_style" onclick="location.href='page2.html';" style="cursor: pointer;">product</div>
HTML;

You'll noticed I used print instead of echo, as print is a better option, in my opinion, for heredocs and nowdocs, as you only ever need to print one at a time.

  • Like 2
Link to comment
Share on other sites

Hi Mike Anthony

Yes, I read about heredocs, actually that looks nice as you don't need to use escape \

and can write the html line as it is. Somehow we are hooked on echo with escapes.

Thanks for the link you included. Now this part of the read in the text of that link is interesting:

Quote:

Heredoc's are a great alternative to quoted strings because of increased readability and maintainability. You don't have to escape quotes and (good) IDE's or text editors will use the proper syntax highlighting.

A VERY common example: echoing out HTML from within PHP:

$html = <<<HTML
<div class='something'>
<ul class='mylist'>
<li>$something</li>
<li>$whatever</li>
<li>$testing123</li>
</ul>
</div>
HTML;

// sometime later
echo $html;

Easy to read. Easy to maintain.

The alternative is echoing quoted strings, which end up containing escaped quotes and IDEs aren't going to highlight the syntax for that language, which leads to poor readability and more difficulty in maintenance.

end quote.

Seems you can also use it in other ways like in sql:

$sql = <<<SQL
SELECT
* FROM table
SQL
;

  • Like 2
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...