Jump to content

wrong if else branching


ajoo
Go to solution Solved by bsmither,

Recommended Posts

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 by ajoo
Link to comment
Share on other sites

  • Solution

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 ?>

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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.

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.