简体   繁体   English

If-Else的异常行为

[英]Unexpected behaviour of If-Else

I tried to show a login/logout link in my header based on the value in session. 我试图根据会话中的值在标题中显示登录/注销链接。 i tried some thing like this 我尝试过这样的事情

<ul class="nav navbar-nav navbar-right text-uppercase">

                        <li><a href="<?php echo \Yii::$app->getUrlManager()->createUrl( [ 'site/contactus' ] ); ?>">Contact</a></li>
                        <li><a href="<?php echo \Yii::$app->getUrlManager()->createUrl( [ 'site/modules' ] ); ?>">FAQ</a></li>
                        <?php
                        $session = Yii::$app->session;
                        $user_id = $session->get('userid');//print_r($user_id);die();
                        if($user_id != null)
                        {?>
                        <li><a href="<?php echo \Yii::$app->getUrlManager()->createUrl( [ 'userdetails/logout' ] ); ?>">Logout</a></li>
                        <?php}
                        else 
                        {?>
                        <li><a href="<?php echo \Yii::$app->getUrlManager()->createUrl( [ 'userdetails/login' ] ); ?>">Login</a></li>
                        <?php } ?>  

                    </ul>

then both links didn't appeare in the header(login/logout). 那么两个链接都没有出现在标题(登录/注销)中。 then after a lot of trying i came up with this code 然后经过大量尝试,我想出了这段代码

   <ul class="nav navbar-nav navbar-right text-uppercase">

                            <li><a href="<?php echo \Yii::$app->getUrlManager()->createUrl( [ 'site/contactus' ] ); ?>">Contact</a></li>
                            <li><a href="<?php echo \Yii::$app->getUrlManager()->createUrl( [ 'site/modules' ] ); ?>">FAQ</a></li>
                            <?php
                            $session = Yii::$app->session;
                            $user_id = $session->get('userid');//print_r($user_id);die();
                            if($user_id != null)
                            {
                            ?>
                            <li><a href="<?php echo \Yii::$app->getUrlManager()->createUrl( [ 'userdetails/logout' ] ); ?>">Logout</a></li>
                            <?php
                            }
                            else
                            {
                            ?>
                            <li><a href="<?php echo \Yii::$app->getUrlManager()->createUrl( [ 'userdetails/login' ] ); ?>">Login</a></li>
                            <?php
                            }
                            ?>              
                        </ul>

the code is actually same but i have added some spaces between the curly brackets'{'. 代码实际上是相同的,但是我在大括号'{'之间添加了一些空格。 And it works as i intended. 它按我的预期工作。 Is space an issue when we use html and yii2 code combined? 当我们结合使用html和yii2代码时,空间会成为问题吗?

This has nothing to do with Yii, it is simply a php syntax problem (you should always have a space after <?php )... 这与Yii无关,它只是一个php语法问题( <?php之后应该始终有一个空格)...

If you want to mix condition and html output, and have a better readability, you should use this : 如果要混合使用条件和html输出,并具有更好的可读性,则应使用以下命令:

<?php if ($user_id != null) : ?>
Output 1
<?php else : ?>
Output 2
<?php endif; ?>

Read more : http://php.net/manual/en/control-structures.alternative-syntax.php 阅读更多: http : //php.net/manual/en/control-structures.alternative-syntax.php

There HAS to be a space after the opening <?php tag, which means <?php} is not valid: 必须开口后的空间<?php标签,这意味着<?php}是无效的:

without space: 没有空间:

$ cat z.php
<?php if(true) {?>
true
<?php} else {?>   // note, no space after <?php
false
<?php }?>
$ php z.php
true
<?php} else {?>
false

with space: 与空间:

$ cat y.php
<?php if(true) {?>
true
<?php } else {?>
false
<?php }?>
$ php y.php
true

Note the difference in output. 注意输出的差异。 This has nothign to do with Yii, and everything to do with your core PHP coding. 这与Yii无关,也与核心PHP编码无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM