PHP quotes inside quotes

27,866

Solution 1

According to php.net

To specify a literal single quote, escape it with a backslash (\). 

It means you could have:

<?php
  echo '<span onclick="$(this).addClass(\'selected\');"> </span>';
?>

Solution 2

Heredoc is the perfect solution for this:

<?php
echo <<< HereDocString
    <span onclick="$(this).addClass('selected');"> </span>
HereDocString;
?>
Share:
27,866
THEJENSMAN
Author by

THEJENSMAN

Updated on February 07, 2020

Comments

  • THEJENSMAN
    THEJENSMAN about 4 years

    Is it possible to put quotes inside quotes?

    If so how?

    Here is my code:

    <?php
    
        echo '<span onclick="$(this).addClass('selected');"> </span>';
    
    ?>