ajoo Posted May 1, 2014 Share Posted May 1, 2014 (edited) Hi ! Can someone take a look at this simple code which worked perfectly till I upgraded to php 5.5.11. Here in my code popo should be either late or great depending upon the variable value ( in this case Great ). It seems to be echoing out both !?? Please can someone point the error ? I seem to be missing it. Thanks ! <?php session_start(); $_SESSION['popo']="POPO"; ?> <html> <head> <title> DYN PAGE </title> <style> .wrapper{ width: 1000px; height: 600px; border: 1px solid #e1e1e1; margin: 10px auto 0 auto; } .header{ width: 1000px; height: 65px; font-size: 17px; font-width: bold; color: #fff; text-align: center; background: #717171; } .lowerheader{ width: 1000px; height: 60px; color: #fff; text-align: center; background: #919191; 'display: table; 'overflow: hidden; } </style> </head> <body> <div class = 'wrapper'> <div class = 'header'> <? if(isset($_SESSION['popo']) && $_SESSION['popo'] == "POPO"): ?> <h2><br> POPO IS GREAT </br></h2> <? else : ?> <h2><br> POPO IS LATE </br></h2> <? endif ; ?> </div> <div class = 'lowerheader'> <p> What ever it takes </P> </div> </div> </body> </html> Edited May 1, 2014 by ajoo Quote Link to comment https://forums.phpfreaks.com/topic/288160-wrong-if-else-branching/ Share on other sites More sharing options...
Solution bsmither Posted May 1, 2014 Solution Share Posted May 1, 2014 It seems you are using PHP's "short open" tags. Your PHP configuration file must specifically allow for that. If it does not, then: <? stuff ?> will be ignored by PHP. It will also be ignored by the browser because angles denote an HTML tag, not content to be shown. But since a question mark is not a proper name of an HTML tag, the browser won't know what to do with it, except to ignore it. Thus, but phrases get displayed. For now, try to always use the normal PHP open tag: <?php stuff ?> Quote Link to comment https://forums.phpfreaks.com/topic/288160-wrong-if-else-branching/#findComment-1477830 Share on other sites More sharing options...
ginerjm Posted May 1, 2014 Share Posted May 1, 2014 Besides the previous post, you might make your code easier to read and your (coding) life easier in general if you do this: <div class = 'header'> <?php if (isset($_SESSION['popo']) && $_SESSION['popo'] == "POPO") { echo "<h2><br> POPO IS GREAT </br></h2>"; } else { echo "<h2><br> POPO IS LATE </br></h2>"; } ?> </div> If you simply must intermingle your html and your php, avoiding the on/off on/off on/off method of doing it is easier in general. Quote Link to comment https://forums.phpfreaks.com/topic/288160-wrong-if-else-branching/#findComment-1477833 Share on other sites More sharing options...
ajoo Posted May 2, 2014 Author Share Posted May 2, 2014 Thanks loads. That was the problem. Will avoid using the short cut. How can I avoid the intermingling of of html and php , say in this very example. Thanks ! Quote Link to comment https://forums.phpfreaks.com/topic/288160-wrong-if-else-branching/#findComment-1477897 Share on other sites More sharing options...
bsmither Posted May 2, 2014 Share Posted May 2, 2014 Actually, you have your PHP fairly well separate from your HTML. This, however: <div class = 'header'> <? if(isset($_SESSION['popo']) && $_SESSION['popo'] == "POPO"): ?> <h2><br> POPO IS GREAT </br></h2> <? else : ?> <h2><br> POPO IS LATE </br></h2> <? endif ; ?> </div> is mixing "data logic" with "display logic". There actually is no display logic required in this example. This variation: <?php session_start(); $_SESSION['popo']="POPO"; if (isset($_SESSION['popo']) && $_SESSION['popo'] == "POPO") {$message = "POPO IS GREAT";} else {$message = "POPO IS LATE";} ?> <!-- snipped --> <div class = 'header'> <h2><?= $message ?></h2> </div> <!-- snipped --> (The PHP is condensed. Refer to post #3.) All the data gathering and preparation logic is performed in the PHP area. All the presentation logic (loops to build selectors, etc) is in the HTML area. Quote Link to comment https://forums.phpfreaks.com/topic/288160-wrong-if-else-branching/#findComment-1477899 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.