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. Edited by kicken
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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.