Page 1 of 1

PHP print/echo table structure

Posted: Mon Oct 18, 2010 6:12 pm
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?

Re: PHP print/echo table structure

Posted: Mon Oct 18, 2010 9:10 pm
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

Re: PHP print/echo table structure

Posted: Mon Oct 18, 2010 11:12 pm
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.

Re: PHP print/echo table structure

Posted: Tue Oct 19, 2010 6:28 am
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;
 

Re: PHP print/echo table structure

Posted: Tue Oct 19, 2010 2:20 pm
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.

Re: PHP print/echo table structure

Posted: Sat Oct 30, 2010 12:00 pm
by ConceptDestiny
Cool! Thanks guys. :)