0

I am checking if the value stored in my variable is equal to 0 or 1.
Depending on the value, I want to display some text inside my html form.
The value that I have inside $details['status'] is 0 of type string.

When I print_r() outside of the if-else structure, the result is 0.
However, if I print_r() inside the if statements, I get nothing back.
I did a var_dump() and the the values is of type string

<form class="form-horizontal" action="/MVC/teacher/becomeTeacher" method="post">
    <?php print_r($details['status'])?> <!-- Gives me 0 -->

    <?php if($details['status'] === 1): ?>
        This will show if the status value is 1.
    <? elseif ($details['status'] === 0): ?>
        Otherwise this will show.
    <?php endif; ?>
</form>

EDIT

I tried multiple options.

Option A - Both if statements execute.

<?php if($details['status'] == 0): ?>
        This will show if the expression is true.
    <? elseif ($details['status'] == 1): ?>
        Otherwise this will show.
    <?php endif; ?>

Option B - Both if statements execute

<?php if($details['status'] === '0'): ?>
        This will show if the expression is true.
    <? elseif ($details['status'] === '1'): ?>
        Otherwise this will show.
    <?php endif; ?>

I found a solution but I find it redundant

<?php if($details['status'] === '1'): ?>
        This will show if the expression is true.

    <?php endif; ?>
    <?php if($details['status'] === '0'): ?>
        Otherwise this will show.
    <?php endif; ?>
3
  • 1
    What is the $details['status'] type? String or Int? Commented May 1, 2017 at 0:03
  • 1
    Have you tried a simple == comparison? Commented May 1, 2017 at 0:10
  • 1
    Did you determine it was an int with var_dump or with the result of print_r? Commented May 1, 2017 at 0:11

2 Answers 2

1

I found the issue.

You are missing the php from <? on the elseif line. It should be <?php unless you have short tags enabled, which I'm guessing you don't.

<?php if($details['status'] == 0): ?>
        This will show if the expression is true.
    <? elseif ($details['status'] == 1): ?>
        Otherwise this will show.
    <?php endif; ?>

Should be:

<?php if($details['status'] == 0): ?>
        This will show if the expression is true.
    <?php elseif ($details['status'] == 1): ?>
        Otherwise this will show.
    <?php endif; ?>
Sign up to request clarification or add additional context in comments.

Comments

0

I think the problem lies in the "===" , === is used to compare for exact type, i think in your case, $detail['status'] = "0" is actually string, so it will not get into any of your if statement.

Here a reference to php comparison operators. Hope it helps.

In your case, either change the if statement to $details['status'] == 0 or $details['status'] === '0' will solve your problem.

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.