View Full Version : Lots of HTML between if and else

05-29-2007, 01: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
//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?

05-29-2007, 01: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."

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

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


05-29-2007, 07: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.

05-29-2007, 07: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.

05-29-2007, 07: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