Atom Editor:当PHP在Javascript中时出错

I have the following code in a PHP page in ATOM:

<html>
  <head>
    <?php $testvar = "Test"; ?>

      <script> 
        var test = <?php echo $testvar; ?>;
      </script>

  </head>

  <body>

  <p> This is a test. </p>

  </body>
</html>

The page is doing exactly as planned on the PHP page but in Atom, everything after the </script> line is highlighting in red as in an error. What's going on here?

Here is a screenshot of the actual code I'm using. The above is an example but also has the same problem.

I have opened a Issue claim on the Atom support but I would like to try here as well to see if it's anything within the code.

Thanks!

Image enter image description here

This is a bug in your code, not in Atom. Your echo statement produces an unenclosed string:

<script> 
    var test = Test;
</script>

This results in a JavaScript error ("Uncaught ReferenceError: Test is not defined") because you are telling the interpreter to use a variable named Test, but none exists.

You need to produce a properly enclosed string, which you can do with json_encode():

<script> 
    var test = <?php echo json_encode($testvar); ?>;
</script>

Based on your comment, you're actually trying to build an array, not just echo a string. You have this code:

var main_categories_array = [<?php echo '"'.implode('","', $main_categories_array ).'"' ?>];

If your array contains any quotation marks, like the simple "Test" example, you will get errors. So, your code should be:

var main_categories_array = <?php echo json_encode($main_categories_array); ?>;

This is a bug with Atom.

Posted here: https://github.com/atom/atom/issues/13532

That was an actual bug fixed in 1.13, released yesterday : 10th of January.

Sources :