...

View Full Version : Lots of HTML between if and else



ScottInTexas
05-29-2007, 02:19 AM
I would like to place a lot of HTML between the if and the else in php. But if I close the php tag doesn't that kill the if/else?



<?php if (thisCondition){
//many lines of HTML without using echo
}else{
//many other lines of HTML
?>


If I put the closing php tag after my lines of html they will generate an error (of course). But if I place the php close tag before the HTML then the else won't be seen. How should I handle this? Lots of "echos" or print?

CFMaBiSmAd
05-29-2007, 02:25 AM
doesn't that kill the if/else?No. Just put a closing php tag, put your "if" HTML, put an opening php tag, put the } else {, put a closing php tag, put your "else" HTML, put an opening php tag, and put the final }.

Short answer - the opening and closing php tags just enter and exit PHP "mode."

iLLin
05-29-2007, 02:34 AM
<?php
//visual representation to what CFMaBiSmAd said
if(this is true) {
?>
<div>Do your true html</div>
<?php
} else {
?>
<div>Do your other html</div>
<?php
}
?>

kewlceo
05-29-2007, 02:35 AM
Reinforcing the good answers above, here's a helpful link:

http://en.wikibooks.org/wiki/Programming:Complete_PHP/Escaping_from_HTML

ScottInTexas
05-29-2007, 08:23 PM
GREAT! I was not thinking clearly. When I used the close tag I had to know that the php didn't get killed, I have variables that are used in other places! Thanks guys.

mlseim
05-29-2007, 08:35 PM
Don't forget about PHP "includes" ...

All of that HTML could be brought in with an include from another
site, page, text file, etc. Perhaps that HTML changes dynamically?

just a thought.

ScottInTexas
05-29-2007, 08:41 PM
Thanks. I was thinking I would have to resort to an include as I do my leftbar and footer. But this is just a case of "If the user has done this then show that otherwise show this."

Now if I can write a simple if correctly all should be well. Nothing like learning from scratch! IMO this is not like ASP at all.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum