correct way to escape a script in an echo

0

You will see I can not successfully escape a script inside an echo, here is my code but I can not make it work, if any could give me an example

echo '
    // Initialize tooltip component
    $(function () {
      $('[data-toggle="tooltip"]').tooltip()
    })
    </script>

</html>';
    
asked by crt 27.10.2018 в 21:16
source

2 answers

0

you could try this concatenation

 echo '<script>
// Initialize tooltip component
$(function () {
  $("[data-toggle= '."'tooltip'".']").tooltip()
})
</script>
';
    
answered by 27.10.2018 в 21:45
0

A common practice to insert HTML fragments in PHP, more if they are long, is to use the opening and closing tags in this way:

<?php
// código PHP
?>
<script>
// Initialize tooltip component
$(function () {
  $('[data-toggle="tooltip"]').tooltip()
})
</script>
<?php
// resto del código...

This form also has the advantage that IDEs correctly recognize HTML and JS and apply syntax highlighting as opposed to if one uses an escaped string.

    
answered by 28.10.2018 в 02:23