简体   繁体   中英

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. 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)" . I am using jQuery library.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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