我可以在 PHP 中的 JavaScript 中编写 PHP 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26179078/
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-10-28 05:36:44  来源:igfitidea点击:

Can I write PHP inside JavaScript inside PHP?

javascriptphp

提问by Malasorte

Inside a PHP script I have this:

在 PHP 脚本中,我有这个:

echo <<<EOD

<script type="text/javascript">
document.getElementById('my_element_id').innerHTML='Do stuff';
</script>

EOD;

Can I add PHP inside the JavaScript? Replace the "Do stuff" part with PHP code? If yes, how do I do it?

我可以在 JavaScript 中添加 PHP 吗?用 PHP 代码替换“做东西”部分?如果是,我该怎么做?

采纳答案by h2ooooooo

First of all, it should be noted that this has nothing to do with javascript. You could have anyform of text. Your actual question is how to use a variable inside of a heredoc.

首先需要说明的是,这与javascript无关。你可以有任何形式的文本。您的实际问题是如何在 heredoc 中使用变量。

Heredocis defined as the following:

Heredoc定义如下:

Nowdocs are to single-quoted strings what heredocs are to double-quoted strings. A nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc. The construct is ideal for embedding PHP code or other large blocks of text without the need for escaping.

Nowdocs 之于单引号字符串就像 Heredocs 之于双引号字符串。nowdoc 的指定与 heredoc 类似,但在 nowdoc 中不进行解析。该构造非常适合嵌入 PHP 代码或其他大文本块而无需转义。

Meaning that since this works:

这意味着因为这有效:

$name = 'Foo';

echo "My name is $name"; // Using double quotes so variables get expanded

Then this also works:

那么这也有效:

$name = 'Foo';

echo <<<EOD
    My name is <strong>$name</strong>
EOD;  // Using heredoc so variables get expanded

Essentially meaning that yes, as long as you put your 'Do stuff'content into a variable first. Note that if you use more advanced variables/arrays, it's a good idea to do a $array = json_encode($array)before pasting it into JS code (imagine if $namewas The Boss's Wife- then the apostrophe would ruin your JS if you don't encode it).

基本上意味着是的,只要您'Do stuff'先将内容放入变量中即可。请注意,如果您使用更高级的变量/数组,最好$array = json_encode($array)在将其粘贴到 JS 代码之前执行 a (想象一下如果$nameThe Boss's Wife- 如果不对其进行编码,撇号会毁掉您的 JS)。

DEMO

演示

回答by Fahad Khan

Can be done like:

可以这样做:

<?php 
$anyphpvariable='foo';
echo <<<EOD 
    <script type="text/javascript">
       document.getElementById('my_element_id').innerHTML=$anyphpvariable;

    </script>
EOD;
?>

Anything from php can be assign to JS and its good practice. But not JS can assign anything to PHP.

来自 php 的任何东西都可以分配给 JS 及其良好实践。但不是 JS 可以为 PHP 分配任何东西。

回答by The One and Only ChemistryBlob

Yes, but I can only get what you want if I place the code after the relevant HTML. You can echo scripts...for example, this will work:

是的,但是如果我将代码放在相关的 HTML 之后,我只能得到你想要的。您可以回显脚本...例如,这将起作用:

<div id="footer">Hey</div>
<?php 
$myVar = 'Hello';
echo "<SCRIPT>
   document.getElementById('footer').innerHTML = '$myVar';
</SCRIPT>";
?> // You will see Hello in the footer

but this will not work:

但这不起作用:

<?php 
$myVar = 'Hello';
echo "<SCRIPT>
   document.getElementById('footer').innerHTML = '$myVar';
</SCRIPT>";
?>
<div id="footer">Hey</div>// You will see Hey in the footer

As a side note, these other functions/methods will work too:

作为旁注,这些其他功能/方法也将起作用:

$myVar = 'Hi';
echo "<SCRIPT>
    alert('$myVar');
</SCRIPT>";//Alerts 'Hi'

$myVar = 'home.php';
echo "<SCRIPT>
    location = '$myVar';
</SCRIPT>";//Takes you to home.php

$myVar = 'hi';
echo "<SCRIPT>
    myFunction('$myVar');
</SCRIPT>";//calls myFunction with argument $myVar