如何用 PHP 输出 JavaScript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1164130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:18:21  来源:igfitidea点击:

How to output JavaScript with PHP

php

提问by Pascal MARTIN

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

我是 PHP 新手。我需要用 PHP 输出以下 JavaScript。这是我的代码:

<html>
<body>
<?php

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

?>
</body>
</html>

But it's showing the error:

但它显示错误:

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /var/www/html/workbench/person/script.php on line 4

解析错误:语法错误,意外的 T_STRING,需要 ',' 或 ';' 在第 4 行的 /var/www/html/workbench/person/script.php 中

Can anyone please help? I also need some simple tutorials on how to use PHP, HTML and JavaScript for an application.

有人可以帮忙吗?我还需要一些关于如何在应用程序中使用 PHP、HTML 和 JavaScript 的简单教程。

回答by Ionu? G. Stan

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:

您应该转义 PHP 字符串中的 JavaScript 字符串分隔符。您正在对 PHP 和 JavaScript 字符串使用双引号。试试这样:

<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:

当字符串分隔符与引号相同时,您必须在 JavaScript 和 PHP 上转义引号:

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

Same in JavaScript:

在 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 "'";

回答by Pascal MARTIN

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"

为避免这种情况,您可以为字符串声明使用另一种语法,称为“ 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.

使用此语法,您可以声明一个长字符串,甚至包含单引号和/或双引号,而不必转义那些;它将使您的 Javascript 代码更易于编写、修改和理解——这总是一件好事。

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.

请注意,使用 Heredoc 语法(如用双引号分隔的字符串),变量是内插的。

回答by Johan

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:

如果您想在 JavaScript 中使用 PHP,请执行以下操作:

<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.

希望这能有所帮助。

回答by Turnor

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

更简单的方法是使用 PHP 的 heredoc 语法。一个例子:

<?php

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

?>

回答by Wil Moore III

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>
    

  1. JavaScript 块在文档中放置得很晚,因此您不必担心元素不存在。

  2. 您正在脚本顶部设置一个 PHP 变量,并仅输出 JavaScript 块中变量的值。

    这样,您就不必担心转义双引号或 HEREDOCS(如果您真的必须去那里,这是推荐的方法)。

    Javascript 嵌入示例

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

回答by Randell

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 Manualfor more info on Strings.

另外,请查看PHP 手册以获取有关字符串的更多信息。

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

另外,为什么要使用 PHP 打印 JavaScript?我觉得你的设计有问题。

回答by Greg

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

或者只是远离 php

<script type="text/javascript">

回答by Maury

You want to do this:

你想这样做:

<html>
<body>
<?php

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

?>
</body>
</html>

回答by mhndev

instead you could easily do it this way :

相反,您可以轻松地这样做:

<html>
<body>
<script type="text/javascript">
<?php
  $myVar = "hello";
?>
   document.write("<?php echo $myVar ?>");
</script>
</body>

回答by tawsif torabi

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,

您正在使用 " 而不是 ' 它混合了 php 语法和 javascript。PHP 将使用 echo 函数打印 javascript,但它将 js 代码视为错误的 php 语法。所以试试这个,

<html>
<body>
<?php

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

?>
</body>
</html>