简体   繁体   English

不能echo php echo作为字符串

[英]Cant echo php echo as a string

i am trying to echo below. 我试图在下面回声。 pure text. 纯文本。

<script src="<?php echo $config['dir_plugins']; ?>qroll.js"></script>

but it wont echo correctly.below is code.somehow it omits the echo as aa string and with it most of the other parts of the string. 但是它不会正确地回显。下面是代码。以某种方式将回显忽略为一个字符串,并且与字符串的其他大部分相同。

<?php
$arr = array("qroll", "validate", "hellios");
$str2 = "src='<?php echo \$config[\'dir_plugins\']; ";
foreach ($arr as &$value) {
    echo $str2;
    echo $value;
}
?>

it just returns src=' 它只是返回src='

This line is strange: 这行很奇怪:

$str2 = "src='<?php echo \$config[\'dir_plugins\']; ";

I think it should be 我认为应该

$str2 = "src='".$config[\'dir_plugins\']."'";

This does what I think you are asking for 这符合我的要求

<?php
$arr = array("qroll", "validate", "hellios");
$str2 = '<script src="<?php echo $config[\'dir_plugins\']; ?>"';
foreach ($arr as &$value) {
    echo $str2 . $value . '.js></script>';
    //echo PHP_EOL;
}
?>

OUTPUT 输出值

<script src="<?php echo $config['dir_plugins']; ?>"qroll.js></script>
<script src="<?php echo $config['dir_plugins']; ?>"validate.js></script>
<script src="<?php echo $config['dir_plugins']; ?>"hellios.js></script>

To avoid PHP evaluating a string, do not use " but ' 为避免PHP计算字符串,请不要使用"而是'

$str2 = 'src=\'<?php echo $config[\'dir_plugins\']; ';

should do the trick 应该可以

Note that if you didnt set the page as text/plain your browser will hide everything after the <? 请注意,如果您未将页面设置为text/plain浏览器将在<?之后隐藏所有内容<? because of the < . 由于< You may want to use the &lt; 您可能要使用&lt; to force it to be printed for debug purpose. 强制将其打印以用于调试目的。

Following on from some comments, it appears that the user acutally wants to echo a string that includes some PHP in it. 根据一些评论,用户似乎很想回显包含PHP的字符串。

If the desired output is: 如果所需的输出是:

src='<?php echo $config['dir_plugins']; ?>

Change your code to: 将您的代码更改为:

$str2 = "src='&lt;?php echo \$config['dir_plugins']; ?&gt;";

This will output: 这将输出:

src='<?php echo $config['dir_plugins']; ?>

Presumably the rest of the output (the script name and script tags) will be added by your code. 大概其余的输出(脚本名称和脚本标签)将由您的代码添加。

You are in effect escaping the HTML-incompatible parts of your output by converting < to &lt; 您实际上是通过将<转换为&lt;来转义输出的HTML不兼容部分&lt; and > to &gt; >&gt;

If you are trying to echo HTML code for display on your page, you can always try htmlspecialchars which will convert < to &lt; 如果您尝试回显HTML代码以在页面上显示,则始终可以尝试htmlspecialchars ,它将<转换为&lt; ;。 and > to &gt; >&gt; for you. 为了你。

<?php
$arr = array("qroll", "validate", "hellios");
$str2 = "src='<?php echo \$config[\'dir_plugins\']; ";
foreach ($arr as &$value) {
    echo htmlspecialchars($str2);
    echo $value;
}

Output in source: 源输出:

src='&lt;?php echo $config[\'dir_plugins\']; qrollsrc='&lt;?php echo $config[\'dir_plugins\']; validatesrc='&lt;?php echo $config[\'dir_plugins\']; hellios

Output on page: 页面输出:

src='<?php echo $config[\'dir_plugins\']; qrollsrc='<?php echo $config[\'dir_plugins\']; validatesrc='<?php echo $config[\'dir_plugins\']; hellios

<?php
$str2 = 'src=\'<?php echo $config[\'dir_plugins\']; ?>\'';
echo htmlspecialchars($str2);

Output on page: 页面输出:

src='<?php echo $config['dir_plugins']; ?>'

Here is an example of everything together: 这是所有事物的示例:

<pre>
<?php
$arr = array('qroll', 'validate', 'hellios');
$str2 = '<script src=\'<?php echo $config[\'dir_plugins\']; ?>';
$str3 = '.js\'></script>';
foreach ($arr as $value) {
    echo htmlspecialchars($str2), $value, htmlspecialchars($str3), "\n";
}
?>
</pre>

Which will look like this on your page: 在您的页面上看起来像这样:

<script src='<?php echo $config['dir_plugins']; ?>qroll.js'></script>
<script src='<?php echo $config['dir_plugins']; ?>validate.js'></script>
<script src='<?php echo $config['dir_plugins']; ?>hellios.js'></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM