if statement – How to I fix this if/else tree? PHP-ThrowExceptions

Exception or error:

I am trying to program a sub-system for students on my website, I tried using these if/else statements to determine some outputs if the parameters match with the user information they have. So if a user is a student they can contact other people that are students and cant contact other users that are not students, and vice versa. The code I tried is below. The problem is that the code shows the error message and the message buttons at the same time whether or not the user is or is not a student. TIP: This PHP code is in a .phtml file, hence the amount of PHP opening and closing tags.

<?php if($dc['user']['student'] !== 0){ ?>
    <?php if(Dc_IsStudent($dc['popover']['user_id']) == true){ ?>
        <div class="user-button user-follow-button"><?php echo Dc_GetFollowButton($dc['popover']['user_id']); ?></div>
        <div class="user-button message-button"><?php echo Dc_GetMessageButton($dc['popover']['user_id']); ?></div>
    <?php } ?>

    <?php if(Dc_IsStudent($dc['popover']['user_id']) == false) { ?>
        <?php echo $dc['lang']['student_contact_warning'];  ?> 
    <?php } ?>
<?php } ?>

<?php if($dc['user']['student'] !== 1){ ?>
    <?php if(Dc_IsStudent($dc['popover']['user_id']) == false){ ?>
        <div class="user-button user-follow-button"><?php echo Dc_GetFollowButton($dc['popover']['user_id']); ?></div>
        <div class="user-button message-button"><?php echo Dc_GetMessageButton($dc['popover']['user_id']); ?></div>
    <?php } ?>

    <?php if(Dc_IsStudent($dc['popover']['user_id']) == true) { ?>
        <?php echo $dc['lang']['student_contact_warning'];  ?> 
    <?php } ?>
<?php } ?>
How to solve:

Do yourself a favour and start to use variables for repeated functions, and look up PHP’s HereDoc to allow you to simplify your code. I think you’ll agree the following is clearer.

The question now is what values you expect in your conditions. Run the following and check that the outputted IsStudent and Student are what you expect.

<?php
$Student=$dc['user']['student'];
$IsStudent=Dc_IsStudent($dc['popover']['user_id']);
$Dc_FollowBut=Dc_GetFollowButton($dc['popover']['user_id']);
$Dc_MessageBut=Dc_GetMessageButton($dc['popover']['user_id']);


echo"IsStudent: $IsStudent<br>";
echo"Student: $Student<br>";


if($Student!=0){
    if($IsStudent==true){
        echo<<<EOC
        <div class="user-button user-follow-button">$Dc_FollowBut</div>
        <div class="user-button message-button">$Dc_MessageBut</div>
EOC;
    }else{
        echo $dc['lang']['student_contact_warning'];
    }
}

if($Student!=1){
    if($IsStudent==false){
        echo<<<EOC
        <div class="user-button user-follow-button">$Dc_FollowBut</div>
        <div class="user-button message-button">$Dc_MessageBut</div>
EOC;
    }else{
        echo $dc['lang']['student_contact_warning'];
    }
}
?>

Answer:

<?php 

    if($dc['user']['student'] != 0){

        if(Dc_IsStudent($dc['popover']['user_id']) == true){ 

            echo '<div class="user-button user-follow-button">'. Dc_GetFollowButton($dc['popover']['user_id']) .'</div>';
            echo '<div class="user-button message-button">'. Dc_GetMessageButton($dc['popover']['user_id']) .'</div>';

        } else { 

            echo $dc['lang']['student_contact_warning'];   

        } 

    } else if($dc['user']['student'] != 1){ 

        if(Dc_IsStudent($dc['popover']['user_id']) == false){ 

            echo '<div class="user-button user-follow-button">'. Dc_GetFollowButton($dc['popover']['user_id']) .'</div>';
            echo '<div class="user-button message-button">'. Dc_GetMessageButton($dc['popover']['user_id']) .'</div>';

        } else {

            echo $dc['lang']['student_contact_warning']; 

        }

    } 

?>

Try putting it in one block, rather than 2 seperate blocks. If-else blocks are meant like that. I would prefer using cases here, though i tried cleaning this up a bit for you to see whats going on.

I agree with @jbes in the other awnser that you should use variables, to make things a lot easier to read. i didn’t do this, but i totally agree on that part.

A typo can be made quickly if you constantly run only if blocks, with else, you can point to a different actions in the same if, without closing the complete block. Now I don’t know if $dc['user']['student'] can only be 0 or 1, but else you can toss away the complete else-if statement and just change it to else to make it more cleaner. Also to check if not 0 leaves open for a lot of interpretation, what if the value is 2 or 48644. it would always trigger both blocks, so you may want to swap it around to check if the value equals 1 or 0.

I hope this points you in the right direction

Answer:

I would first make the PHP code more clear. Then you will see that you didn’t close some tags.

<?php
if ($dc['user']['student'] !== 0) { 
   if (Dc_IsStudent($dc['popover']['user_id']) == true) { 
     echo '<div class="user-button user-follow-button">';
     echo Dc_GetFollowButton ($dc['popover']['user_id']); 
     echo '</div>' . "\n";
     echo '<div class="user-button message-button">';
     echo Dc_GetMessageButton($dc['popover']['user_id']);
     echo '</div>' . "\n";
   } else {
     echo $dc['lang']['student_contact_warning']; 
   }
}

if ($dc['user']['student'] !== 1){
  if (Dc_IsStudent($dc['popover']['user_id']) == false){
    echo '<div class="user-button user-follow-button">';
    echo Dc_GetFollowButton($dc['popover']['user_id']);
    echo '</div>' . "\n";
    echo '<div class="user-button message-button">';
    echo Dc_GetMessageButton($dc['popover']['user_id']); 
    echo '</div>' . "\n";
  } else {
    echo $dc['lang']['student_contact_warning'];
  }
}
?>

Now we can start analyzing the code.

Leave a Reply

Your email address will not be published. Required fields are marked *