简体   繁体   中英

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='

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 '

$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 <? because of the < . You may want to use the &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.

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; and > to &gt;

If you are trying to echo HTML code for display on your page, you can always try htmlspecialchars which will convert < to &lt; and > to &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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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