PHP print/echo table structure

C++, C#, Java, PHP, ect...
Post Reply
ConceptDestiny
Posts: 261
Joined: Wed Apr 28, 2010 8:35 am

PHP print/echo table structure

Post by ConceptDestiny »

Hi there,

I noticed halls was using the following structure to assemble a HTML table:

Code: Select all

echo "<table><tr>";
echo "<td></td>";
echo "</tr></table>";
Would there be any inherent issue with doing the following:

Code: Select all

echo "
<table><tr>
<td></td>
</tr></table>"
;
Basically line breaking within an echo or print?
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: PHP print/echo table structure

Post by hallsofvallhalla »

nope! and I recommend it

only reason I do it that way in videos and tutorials is to make it easier to understand
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: PHP print/echo table structure

Post by Jackolantern »

Isn't there some alternative method as well? I can't remember what the keyword is called, but you use it and create an end label, type in as much verbatim HTML as you want, and then use your end label to break back into PHP. I never use it, but someone mentioned it on here one time.
The indelible lord of tl;dr
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: PHP print/echo table structure

Post by Chris »

I recommend the second method as it actually takes less loading time.

A few different methods:

\n or \r make new link breaks. Just like \t a tab.

Code: Select all

echo "<table> <tr>\n <td></td> \n</tr> </table>";
 
No need for an echo here:

Code: Select all

<?php if( $var == $val ) : ?>
<table>
    <tr>
        <td></td>
    </tr>
</table>
<?php endif; ?>
Jackolantern wrote:Isn't there some alternative method as well? I can't remember what the keyword is called, but you use it and create an end label, type in as much verbatim HTML as you want, and then use your end label to break back into PHP. I never use it, but someone mentioned it on here one time.
I think you mean this method? It isn't normally supported in PHP. Is actually used in Perl. It's called EOF.

Code: Select all

echo <<< KEYWORD
<table>
    <tr>
        <td></td>
    </tr>
</table>
KEYWORD;
 
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: PHP print/echo table structure

Post by Jackolantern »

Chris wrote:No need for an echo here:

Code: Select all

<?php if( $var == $val ) : ?>
<table>
    <tr>
        <td></td>
    </tr>
</table>
<?php endif; ?>
Yep, that is the method I use! Only downside is that it can start to get a bit ugly if you do have a lot of HTML, but have to periodically jump back into PHP. However, that can be at least partially fixed by something like Smarty for big projects.
Chris wrote:
Jackolantern wrote:Isn't there some alternative method as well? I can't remember what the keyword is called, but you use it and create an end label, type in as much verbatim HTML as you want, and then use your end label to break back into PHP. I never use it, but someone mentioned it on here one time.
I think you mean this method? It isn't normally supported in PHP. Is actually used in Perl. It's called EOF.

Code: Select all

echo <<< KEYWORD
<table>
    <tr>
        <td></td>
    </tr>
</table>
KEYWORD;
 
And yes, that is the one I was thinking of! I had actually never used it before.
The indelible lord of tl;dr
ConceptDestiny
Posts: 261
Joined: Wed Apr 28, 2010 8:35 am

Re: PHP print/echo table structure

Post by ConceptDestiny »

Cool! Thanks guys. :)
Post Reply

Return to “Coding”