简体   繁体   English

这个php语法有什么问题?

[英]What's wrong with this php syntax?

I am getting error in my browsers console while executing this code :- 执行此代码时,我的浏览器控制台出现错误:-

    <script type="text/javascript">
       $(function(){
           if(<?php (strpos($_SERVER['REQUEST_URI'],'/web/login/index/1') === 0)?>) {
              var loggedOut= <?=$fb_logout;?>;
            }
           else{
             var loggedOut = null;  
            }

        some more java script
        .
        .
        .


        .  
      </script>  

What I am trying to do is to initialise the loggedOut with a value 1 which is stored in fb_logout if the request comes from a page having /web/login/index/1 part of URl. 我要执行的操作是,如果请求来自具有URl的/ web / login / index / 1部分的页面,则使用值1初始化loggingOut,该值存储在fb_logout中。 I am getting error in the browsers console saying "Syntax error" pointing just after "if()" closing parantheses saying "jquery.min.js (line 3, col 15)" . 我在浏览器控制台中收到错误消息,指出“语法错误”指向“ if()”后紧跟在括号中,并指出“ jquery.min.js(第3行,第15行)”。 I am using jQuery library. 我正在使用jQuery库。

Thanks 谢谢

您需要返回/显示语句的结果:

<?=((strpos($_SERVER['REQUEST_URI'],'/web/login/index/1') === 0) ? 'true': 'false')?>

Main answer: You're not outputing the result of your comparison. 主要回答:您没有输出比较结果。

Suggestion: How about using the ternary operator? 建议:如何使用三元运算符?

 var loggedOut = <?php echo (strpos($_SERVER['REQUEST_URI'],'/web/login/index/1') === 0) ? $fb_logout :  'null' ?>

Now, try to check this: 现在,尝试检查以下内容:

<?php
if (strpos($_SERVER['REQUEST_URI'], '/web/login/index/1') === 0) {
    echo "false";
} else {
    echo "true";
}
?>

As the very first PHP code on your code above. 作为上述代码中的第一个PHP代码。

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

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