0

I tried to show a different content based on detecting user's browser language. 1.It seems I can get the language setting value from javascript. 2.I can also echo it out in php. But for some reason, when I out it inside "if statement" (see below), it seems not working? Which part I did wrong? Thanks in advance.

<script>
              let test_lang;
              test_lang = navigator.language;
              if(test_lang == "en"){
                console.log("yes, this is en");
                document.writeln(test_lang);
              }
              <?php
               $test_lang ="<script>document.write(test_lang);</script>";
               ?>
            </script>


            <?php
            echo '$test_lang:'.$test_lang;

            if($test_lang == 'en'){
              echo 'php en';
            }
?>
  • Please remember that php executes on the server, _before_ the JavaScript. You can't just intermingle them like this. It's unclear what you're trying to achieve by doing so, anyway. – ADyson Dec 31 '21 at 23:07
  • `echo 'php en';` will never execute because $test_lang will never equal "en", its value will always be a string of JavaScript code – ADyson Dec 31 '21 at 23:09
  • In your code, ```$test_lang``` always equals the same string: `````` – Anibal E. Alvarez Sifontes Dec 31 '21 at 23:14

0 Answers0