2

I am trying to figure out how I should structure an if/else statement.

I understand the basic if/esle should be as follows;

<?php
if ($a > $b) {
    echo "a is bigger than b";
} elseif ($a == $b) {
    echo "a is equal to b";
} else {
    echo "a is smaller than b";
}
?>

I'm not sure how to implement this logic into my current statement. I think because the php is mixed within html it's confusing me :/

I'm sure this is very wrong but could somebody please tell me how it should be structured?

  <form role="form">
    <div class="btn-group" data-toggle="buttons">
      <label class="btn btn-default <?php if ($status == '1' ) echo 'active btn-success' ; ?>">
        <input type="radio" name="status" id="option1" value="1" autocomplete="off"> Active
      </label>
      <label class="btn btn-default <?php elseif ($status == '2' ) echo 'active btn-warning' ; ?>">
        <input type="radio" name="status" id="option2" value="2" autocomplete="off"> Inactive
      </label>
      <label class="btn btn-default <?php else ($status == '3') echo 'active btn-danger' ; ?>">
        <input type="radio" name="status" id="option3" value="3" autocomplete="off"> Not Found
      </label>
    </div>
  </form>

The error I see in NetBeans is;

Syntax error: unexpected elseif

Can I or should I just use an if statement on each label?

2
  • 1
    Why elseif & else. Use if in each label. Commented Feb 12, 2016 at 10:33
  • Not entirely sure, but you might wanna use open/close brackets for the if / elseif / else statements, it might fix your error. Commented Feb 12, 2016 at 10:34

4 Answers 4

5

Ravi Hirani has already answered the question, but I thought i'd explain why you are getting the error.

The reason is that you are not using the correct syntax. If we pull out the PHP and strip off the HTML, you might be able to see why:

if ($status == '1' ) echo 'active btn-success' ;
elseif ($status == '2' ) echo 'active btn-warning' ;
else ($status == '3') echo 'active btn-danger' ;

As you can see, you are just missing the brackets to complete the syntax required for the if statements. It should look like (without the markup):

if ($status == '1') {
    echo 'active btn-success';
} elseif ($status == '2') {
    echo 'active btn-warning';
} else ($status == '3') {
    echo 'active btn-danger';
}

Now, when including HTML, that can get quite messy! Especially when trying to track opening and closing brackets across many lines of markup. So I usually use the alternate syntax when working with markup, just to make it a little tidier! Which can be found here:

http://php.net/manual/en/control-structures.alternative-syntax.php

All you simply do is swap out the brackets with the alternative characters:

if ($status == '1'):
    echo 'active btn-success';
elseif ($status == '2'):
    echo 'active btn-warning';
else ($status == '3'):
    echo 'active btn-danger';
endif;
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the explanation! I think the alternative characters was what I was trying to get at. This seems like the best approach as I have a lot of html :)
No problem, glad I could help! :)
@RaviHirani: I try, haha
2

Why else and elseif. Either one is only going to set your condition.

<form role="form">
    <div class="btn-group" data-toggle="buttons">
      <label class="btn btn-default <?php if ($status == '1' ) echo 'active btn-success' ; ?>">
        <input type="radio" name="status" id="option1" value="1" autocomplete="off"> Active
      </label>
      <label class="btn btn-default <?php if ($status == '2' ) echo 'active btn-warning' ; ?>">
        <input type="radio" name="status" id="option2" value="2" autocomplete="off"> Inactive
      </label>
      <label class="btn btn-default <?php if ($status == '3') echo 'active btn-danger' ; ?>">
        <input type="radio" name="status" id="option3" value="3" autocomplete="off"> Not Found
      </label>
    </div>
</form>

Comments

2

Use only If condition in each label.

<form role="form">
    <div class="btn-group" data-toggle="buttons">
      <label class="btn btn-default <?php if ($status == '1' ) echo 'active btn-success' ; ?>">
        <input type="radio" name="status" id="option1" value="1" autocomplete="off"> Active
      </label>
      <label class="btn btn-default <?php if ($status == '2' ) echo 'active btn-warning' ; ?>">
        <input type="radio" name="status" id="option2" value="2" autocomplete="off"> Inactive
      </label>
      <label class="btn btn-default <?php if ($status == '3') echo 'active btn-danger' ; ?>">
        <input type="radio" name="status" id="option3" value="3" autocomplete="off"> Not Found
      </label>
    </div>
</form>

You can also do it by below way but it is not recommended.

<?php 
$var = '';  
if ($status == '1' ){
  $var = 'active btn-success';
}elseif($status == '2'){
  $var = 'active btn-success';
}else{
  $var = 'active btn-danger';
}    
?>

<form role="form">
    <div class="btn-group" data-toggle="buttons">
      <label class="btn btn-default <?php echo $var; ?>">
        <input type="radio" name="status" id="option1" value="1" autocomplete="off"> Active
      </label>
      <label class="btn btn-default <?php echo $var; ?>">
        <input type="radio" name="status" id="option2" value="2" autocomplete="off"> Inactive
      </label>
      <label class="btn btn-default <?php echo $var; ?>">
        <input type="radio" name="status" id="option3" value="3" autocomplete="off"> Not Found
      </label>
    </div>
</form>

Hope it will help you :)

Comments

1

A slightly different approach to this could be a ternary operator and it can be used like this:

<form role="form">
    <div class="btn-group" data-toggle="buttons">
      <label class="btn btn-default <?php echo ($status == '1' ) ? 'active btn-success' : ''; ?>">
        <input type="radio" name="status" id="option1" value="1" autocomplete="off"> Active
      </label>
      <label class="btn btn-default <?php echo ($status == '2' ) ? 'active btn-warning' : ''; ?>">
        <input type="radio" name="status" id="option2" value="2" autocomplete="off"> Inactive
      </label>
      <label class="btn btn-default <?php echo ($status == '3' ) ? 'active btn-danger' : ''; ?>">
        <input type="radio" name="status" id="option3" value="3" autocomplete="off"> Not Found
      </label>
    </div>
</form>

The ternary operator is basically a quick if / else.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.