Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Posts
    567
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Lots of HTML between if and else

    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?

    Code:
    <?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?
    Last edited by ScottInTexas; 05-29-2007 at 01:20 AM. Reason: Stupid mistake
    Scott Stewart
    Always happy to learn from pros.

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,026
    Thanks
    2
    Thanked 315 Times in 307 Posts
    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."
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    PHP Code:
    <?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
    }
    ?>

  • #4
    Regular Coder kewlceo's Avatar
    Join Date
    Mar 2006
    Location
    California, US
    Posts
    484
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Reinforcing the good answers above, here's a helpful link:

    http://en.wikibooks.org/wiki/Program...ping_from_HTML
    UBERHOST.NET
    Shared, reseller, semidedicated hosting and dedicated server plans.
    DirectAdmin • Installatron • Money-Back Guarantee • 24/7 Support
    Providing "Service Above All Else" since 2005.

  • #5
    Regular Coder
    Join Date
    Nov 2002
    Posts
    567
    Thanks
    2
    Thanked 4 Times in 4 Posts
    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.
    Scott Stewart
    Always happy to learn from pros.

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,471
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    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.

  • #7
    Regular Coder
    Join Date
    Nov 2002
    Posts
    567
    Thanks
    2
    Thanked 4 Times in 4 Posts
    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.
    Scott Stewart
    Always happy to learn from pros.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •