How to output JavaScript with PHP

前端 未结 12 1501
无人及你
无人及你 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:39

    Another option is to do like this:

    <html>
        <body>
        <?php
        //...php code...  
        ?>
        <script type="text/javascript">
            document.write("Hello World!");
        </script>
        <?php
        //....php code...
        ?>
        </body>
    </html>
    

    and if you want to use PHP inside your JavaScript, do like this:

    <html>
        <body>
        <?php
            $text = "Hello World!";
        ?>
        <script type="text/javascript">
            document.write("<?php echo $text ?>");
        </script>
        <?php
        //....php code...
        ?>
        </body>
    </html>
    

    Hope this can help.

    0 讨论(0)
  • 2020-11-27 06:40

    The error you get if because you need to escape the quotes (like other answers said).

    To avoid that, you can use an alternative syntax for you strings declarations, called "Heredoc"

    With this syntax, you can declare a long string, even containing single-quotes and/or double-quotes, whithout having to escape thoses ; it will make your Javascript code easier to write, modify, and understand -- which is always a good thing.

    As an example, your code could become :

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

    Note that with Heredoc syntax (as with string delimited by double-quotes), variables are interpolated.

    0 讨论(0)
  • 2020-11-27 06:42

    The following solution should work quite well for what you are trying to do.

    1. The JavaScript block is placed very late in the document so you don't have to worry about elements not existing.

    2. You are setting a PHP variable at the top of the script and outputting just the value of the variable within the JavaScript block.

      This way, you don't have to worry about escaping double-quotes or HEREDOCS (which is the recommended method if you REALLY must go there).

      Javascript Embedding Example

      <div id="helloContainer"><div>
      
      <script type="text/javascript">
          document.getElementById('helloContainer').innerHTML = '<?= $greeting; ?>';
      </script>
      

    0 讨论(0)
  • 2020-11-27 06:46

    An easier way is to use the heredoc syntax of PHP. An example:

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

    Try This:

    <html>
    <body>
    <?php
    
    echo "<script type="text/javascript">";
    echo "document.write("Hello World!");";
    echo "</script>";
    
    ?>
    </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题