Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single Quoted String Over Multiple Lines

I am working through this PHP & MySQL book and I have run into something quite different than what I am used to when it comes to echoing multiple lines of a single quoted string.

The example given is this:

    echo '<table align="center" cellspacing="0" cellpadding="5" width="75%">
        <tr>
          <td align="left"><b>This</b></td>
          <td align="left"><b>That</b></td>
          <td align="left"><b>Then</b></td>
          <td align="left"><b>When</b></td>
          <td align="left"><b>What</b></td>
        </tr>';

Obviously I could break out of PHP and use HTML, but this isn't always an option. And when I don't have that option I would usually break up my string for easier reading as such:

    echo '<table align="center" cellspacing="0" cellpadding="5" width="75%">' . 
      '<tr>'
        '<td align="left"><b>This</b></td>' . 
        '<td align="left"><b>That</b></td>' . 
        '<td align="left"><b>Then</b></td>' . 
        '<td align="left"><b>When</b></td>' . 
        '<td align="left"><b>What</b></td>' . 
      '</tr>';

The author of this book is well respected in the php community, and the book comes highly recommended and is reviewed well... So my question is this:

Is There anything wrong with his method and should this be practiced?

like image 889
jremydeaton Avatar asked Dec 27 '22 23:12

jremydeaton


2 Answers

No, there's nothing wrong with it at all. Just be aware that the spaces, tabs and line returns in the quoted string will be output as well. That's rarely a problem - just something to remember.

To me, it's a little easier to read and a little harder to get wrong than your way. In fact, your example has a syntax error. Can you find the missing dot?

like image 63
Scott Saunders Avatar answered Dec 30 '22 09:12

Scott Saunders


There are even more possibilities

?>
<table align="center" cellspacing="0" cellpadding="5" width="75%">
    <tr>
      <td align="left"><b>This</b></td>
      <td align="left"><b>That</b></td>
      <td align="left"><b>Then</b></td>
      <td align="left"><b>When</b></td>
      <td align="left"><b>What</b></td>
    </tr>
<?php

or

echo <<<HTML
<table align="center" cellspacing="0" cellpadding="5" width="75%">
    <tr>
      <td align="left"><b>This</b></td>
      <td align="left"><b>That</b></td>
      <td align="left"><b>Then</b></td>
      <td align="left"><b>When</b></td>
      <td align="left"><b>What</b></td>
    </tr>
HTML;

The first one is recognized as HTML in every IDE, the second one at least in PHPStorm, and thus IDEs can highlight it using the HTML-syntax-highlighting, which is a huge benefit compared to your both examples (Sidenote: PHPStorm highlights html in strings too, thus it's not that bad ;) But as far as I remember it cannot validate html in strings)

Additional you should separate output- from processing-code

like image 35
KingCrunch Avatar answered Dec 30 '22 11:12

KingCrunch