douliao5467 2017-04-30 23:59
浏览 192
已采纳

PHP中的PHP if语句

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; ?>
  • 写回答

2条回答 默认 最新

  • dongqiao1158 2017-05-01 00:44
    关注

    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; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改