Jump to content

Nested if's and HTML/other code.


CB150Special

Recommended Posts

All the examples I have seen use a single if statement to then execute some other code. When there is more that one, how do you code it or can't it be done ?

 

This does not work and I think the syntax is theoretically correct but practically, not.

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php if ('b'=='b') {
            if ('a'=='a'):?> 
                 <a href="index_login.php">New Page</a>  
            <?php endif; ?>
        }     
    </body>
</html>
Link to comment
Share on other sites

You can either combine your conditions using logic operators or you can use multiple if/else if/else statement to complete your logic.

 

For example:

<body>
   <?php if ('b'=='b' && 'a'=='a'):?> 
      <a href="index_login.php">New Page</a>  
   <?php endif; ?>     
</body>
or

<body>
   <?php if ('b'=='b'):?> 
      <a href="index_login.php">New Page</a>  
   <?php elseif ('a' == 'a'): ?>
      <a href="index_login.php">Another New Page</a>  
   <?php endif; ?>     
</body>
To nest you need to ensure you match up your opening and closing braces/if-endif's. This is where you failed in your example, your } is not within PHP tags.

<body>
   <?php if ('b'=='b'):?> 
      <?php if ('a' == 'a'): ?>
         <a href="index_login.php">New Page</a>  
      <?php endif; ?>
   <?php endif; ?>     
</body>
Don't mix the traditional and alternative syntax in the same file. Pick one and use it consistently. Alternative style is generally used in template applications and traditional everywhere else.
Link to comment
Share on other sites

Thanks for the reply. It's concepts I working with here at this stage, not necessary code.

From my reading, if endif seems to be use with included HTML etc, and if {} for normal code.

 

I've just done some experiments and it appears the mixing the two types gives you errors. Nesting all {} or if endif works but not a combination of the two.

 

Does this seem correct ?

Link to comment
Share on other sites

No. The variants can be combined in any possible way -- this is how syntax works. If you got a different result, then your experiments are wrong.

<?php

// this is stupid, but it's valid

if (true):
    if (true)
    {
        echo 1;
    }
endif;

if (true)
{
    if (true):
        echo 2;
    endif;
}

In your above code, you apparently got confused while jumping in and out of PHP mode. Your closing "}" is in the HTML markup, so the PHP code is incomplete.

 

Anyway, none of this matters, because as kicken already pointed out, this is bad style to begin with.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.