CB150Special Posted July 11, 2017 Share Posted July 11, 2017 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> Quote Link to comment Share on other sites More sharing options...
kicken Posted July 11, 2017 Share Posted July 11, 2017 (edited) 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 July 11, 2017 by kicken Quote Link to comment Share on other sites More sharing options...
CB150Special Posted July 11, 2017 Author Share Posted July 11, 2017 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 ? Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted July 11, 2017 Share Posted July 11, 2017 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.