How to output JavaScript with PHP

前端 未结 12 1502
无人及你
无人及你 2020-11-27 06:16

I am new to PHP. I need to output the following JavaScript with PHP. This is my code:





        
相关标签:
12条回答
  • 2020-11-27 06:52

    You should escape the JavaScript string delimiters inside the PHP string. You're using double quotes for both PHP and JavaScript strings. Try like this instead:

    <html>
    <body>
    <?php
    
    // Here, we use single quotes for PHP and double quotes for JavaScript
    echo '<script type="text/javascript">';
    echo 'document.write("Hello World!")';
    echo '</script>';
    
    ?>
    </body>
    </html>
    

    You have to escape quotes on both JavaScript and PHP when the string delimiter are the same as the quotes:

    echo "\""; // escape is done using a backslash
    echo '\'';
    

    Same in JavaScript:

    alert("\""); // escape is done using a backslash
    alert(echo '\'');
    

    But because it's very hard to read a string with such escape sequences, it is better to combine single with double quotes, as needed:

    echo '"';
    echo "'";
    
    0 讨论(0)
  • 2020-11-27 06:52

    You are using " instead of ' It is mixing up php syntax with javascript. PHP is going to print javascript with echo function, but it is taking the js codes as wrong php syntax. so try this,

    <html>
    <body>
    <?php
    
    echo "<script type='text/javascript'>";
    echo "document.write('Hello World!')";
    echo "</script>";
    
    ?>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-11-27 06:58

    You need to escape the double quotes like this:

    echo "<script type=\"text/javascript\">";
    echo "document.write(\"Hello World!\")";
    echo "</script>";
    

    or use single quotes inside the double quotes instead, like this:

    echo "<script type='text/javascript'>";
    echo "document.write('Hello World!')";
    echo "</script>";
    

    or the other way around, like this:

    echo '<script type="text/javascript">';
    echo 'document.write("Hello World!")';
    echo '</script>';
    

    Also, checkout the PHP Manual for more info on Strings.

    Also, why would you want to print JavaScript using PHP? I feel like there's something wrong with your design.

    0 讨论(0)
  • 2020-11-27 07:00

    You want to do this:

    <html>
    <body>
    <?php
    
    print '
    <script type="text/javascript">
    document.write("Hello World!")
    </script>
    ';
    
    ?>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-11-27 07:02

    instead you could easily do it this way :

    <html>
    <body>
    <script type="text/javascript">
    <?php
      $myVar = "hello";
    ?>
       document.write("<?php echo $myVar ?>");
    </script>
    </body>
    
    0 讨论(0)
  • 2020-11-27 07:06

    You need to escape your quotes.

    You can do this:

    echo "<script type=\"text/javascript\">";
    

    or this:

    echo "<script type='text/javascript'>";
    

    or this:

    echo '<script type="text/javascript">';
    

    Or just stay out of php

    <script type="text/javascript">
    
    0 讨论(0)
提交回复
热议问题