2017-04-30 17 views
0

私の変数に格納されている値が0または1であるかどうかを確認しています。
HTMLのフォームの中にテキストを表示します。
$ details ['status']の中にある値は、文字列型の0です。

if-else構造体の外側でprint_r()を実行すると、結果は0になります。
if print_r()if文の中では何も返されません。
Iはのvar_dump()および値は、私は複数のオプションを試みHTML内のPHP ifステートメント

型ストリング

<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 - 両方の文が

<?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

これは 'var_dump'または' print_r'の結果でintであると判断しましたか? – 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'に変更すると問題が解決します。

関連する問題