3

I am currently working on a Webproject for my school which is build with HTML, PHP and SQL Databases for dynamic content. Until now everything works out pretty good but I have reached a point where I have to echo something out which contains many Characters like '' and "" which pretty much makes it impossible to use PHP echo with those starting tags ('' and ""). Is there any other way to start a PHP echo ?

if ($rows[$number]['kulturschule'] == 1) {

echo '<div class="tp-caption tp-resizeme hover-scale"
              data-x="center"
              data-y="center"
              data-voffset="[290, 290, 250, 210]"
              data-hoffset="0"
              data-frames='[{"delay":1000,"speed":2000,"frame":"0","from":"sX:0.9;sY:0.9;opacity:0;fb:20px;","to":"o:1;fb:0;","ease":"Power3.easeInOut"},{"delay":"wait","speed":500,"frame":"999","to":"sX:0.9;sY:0.9;opacity:0;fb:20px;","ease":"Power3.easeInOut"}]'
              style="z-index: 20; max-width: auto; max-height: auto; white-space: nowrap;"><a href="http://www.km-bw.de/Kulturschule"><img src="img/logo/kulturschule.jpg"></a>        ';
1
  • 3
    use HEREDOC!
    – Jeff
    Commented Feb 26, 2018 at 12:50

6 Answers 6

2

This is a perfect situation to use HEREDOC:

// put all the html in a variable:
$html = <<<EOT
     <div class="tp-caption tp-resizeme hover-scale"
          data-x="center"
          data-y="center"
          data-voffset="[290, 290, 250, 210]"
          data-hoffset="0"
          data-frames='[{"delay":1000,"speed":2000,"frame":"0","from":"sX:0.9;sY:0.9;opacity:0;fb:20px;","to":"o:1;fb:0;","ease":"Power3.easeInOut"},{"delay":"wait","speed":500,"frame":"999","to":"sX:0.9;sY:0.9;opacity:0;fb:20px;","ease":"Power3.easeInOut"}]'
          style="z-index: 20; max-width: auto; max-height: auto; white-space: nowrap;"><a href="http://www.km-bw.de/Kulturschule"><img src="img/logo/kulturschule.jpg"></a>
EOT;
// note, that EOT; has to be at the very start of the line.

// then:
echo $html;
1

I have reached a point where I have to echo something out which contains many Characters like '' and "" which pretty much makes it impossible to use PHP echo with those starting tags

Then you should escape those characters inside. cf. http://php.net/string

When you look for an alternative, you can use HEREDOC/NOWDOC syntax (see link above).

1

Yes you have the possiblity to use this format:

$text = <<<EOT
  Place your text between the EOT. It's
  the delimiter that ends the text
  of your multiline string.
  $var
EOT;

If you want to use raw strings use this format:

$var = "foo";
$text = <<<'EOT'
  My $var
EOT;

This will ignore the $var and print it as-is

Note: You can not indent the EOT;

1

Another solution is to go way old-school and use php like it was used 10 years ago:

<?php 
if ($rows[$number]['kulturschule'] == 1) {
?>
<div class="tp-caption tp-resizeme hover-scale"
              data-x="center"
              data-y="center"
              data-voffset="[290, 290, 250, 210]"
              data-hoffset="0"
              data-frames='[{"delay":1000,"speed":2000,"frame":"0","from":"sX:0.9;sY:0.9;opacity:0;fb:20px;","to":"o:1;fb:0;","ease":"Power3.easeInOut"},{"delay":"wait","speed":500,"frame":"999","to":"sX:0.9;sY:0.9;opacity:0;fb:20px;","ease":"Power3.easeInOut"}]'
              style="z-index: 20; max-width: auto; max-height: auto; white-space: nowrap;"><a href="http://www.km-bw.de/Kulturschule"><img src="img/logo/kulturschule.jpg"></a>
<?php } ?>
1
  • 1
    Which even though its been there as a solution for years... doesn't make it any less useful ;) I still see it used today in enterprise solutions, as its "just much easier to deal with". Note this really only works for echoing out data... now if he wanted to assign it into a variable to echo later, then yeah... this method doesn't work as great. Commented Feb 26, 2018 at 14:36
0

You can use the following way to escape quotes inside echo :

if ($rows[$number]['kulturschule'] == 1) {

echo "<div class='tp-caption tp-resizeme hover-scale'
              data-x='center'
              data-y='center'
              data-voffset='[290, 290, 250, 210]'
              data-hoffset='0'
              data-frames='[{\"delay\":1000,\"speed\":2000,\"frame\":\"0\",\"from\":\"sX:0.9;sY:0.9;opacity:0;fb:20px;\",\"to\":\"o:1;fb:0;\",\"ease\":\"Power3.easeInOut\"},{\"delay\":\"wait\",\"speed\":500,\"frame\":\"999\",\"to\":\"sX:0.9;sY:0.9;opacity:0;fb:20px;\",\"ease\":\"Power3.easeInOut\"}]";
0

You can escape charachters:

\' single quote
\" double quote

Not the answer you're looking for? Browse other questions tagged or ask your own question.