Reply to Re: Best way to conditionally show large blocks of HTML

Your name:

Reply:


Posted by Toby Inkster on 01/18/07 12:57

Kimmo Laine wrote:

> <?php if (bShowBlock){ ?>
>
> LARGE BLOCK OF HTML
>
> <?php } else { ?>
>
> SOME OTHER LARGE BLOCK OF HTML
>
> <? } ?>

Personally, I've never found that very aesthetically pleasing. I prefer
things between '<?php' and '?>' to seem to make sense on their own (even
if they do, say, access functions or variables defined elsewhere).

Slightly nicer looking are:

<?php
include (bShowBlock ? 'foo.html' : 'bar.html');
?>

or, if you don't want to have the blocks of HTML stored in external files,
you could use output buffering, like so:

<?php
ob_start();
?>
LARGE BLOCK OF HTML
<?php
$foo = ob_get_clean();
ob_start();
?>
SOME OTHER LARGE BLOCK OF HTML
<?php
$bar = ob_get_clean();

print (bShowBlock ? $foo : $bar);
?>

However, if the "large blocks of HTML" actually contain some PHP code, be
aware that the latter of these two will execute both sets of code fully.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация