2017-04-30 26 views
0

PHP我正在检查存储在我的变量中的值是否等于0或1.
根据值,我想在我的html表单中显示一些文本。
我在$ details ['status']里面的值是字符串类型的0。

当我print_r()在if-else结构之外时,结果为0.
但是,如果我在if语句中使用了print_r(),那我什么都不会回来。
我做了的var_dump()和所述值是字符串类型如果HTML中的语句为

<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

我试图多个选项的。

选项A - 两个if语句都执行。

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

选项B - 两个if语句执行

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

我找到了解决办法,但我觉得它多余

<?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; ?> 
+1

什么是'$细节[ '状态']'型?字符串还是Int? –

+1

你有没有试过简单的'=='比较? –

+1

您是否确定这是一个int'var_dump'或者'print_r'的结果? – chris85

回答

1

我发现了这个问题。

您在elseif行缺少了来自<?的php。它应该是<?php,除非你有短标签启用,我猜你没有。

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

应该是:

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

谢谢你是这个问题 – Viteazul

0

我认为问题出在 “===” ===用于比较确切类型,我认为在你的情况下,$ detail ['status'] =“0”实际上是字符串,所以它不会进入你的任何if语句。

Here a reference to php comparison operators。希望能帮助到你。

对于您的情况,将if语句更改为$details['status'] == 0 or $details['status'] === '0'将解决您的问题。