Mohamed Athif Mohamed Athif - 2 months ago 5
PHP Question

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

Answer

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;

Comments