Nadine Ah Nadine Ah - 4 months ago 7
PHP Question

How to put a PHP line in a PHP code using PHP?

I think it will be a weird question but it will help me a lot if I get the right answer!

I have a very simple PHP code ( Just an example ):

<html>
<body>
<?php
echo "My first text bla bla bla";
print "My second text bla bla bla";
?>
</body>
<html>


Now my weird question is:

Can I echo ( print "My second text bla bla bla"; ) in the php code ?
I try it of course using echo but I don't want it to echo as a text but as a real PHP line.

For example I can put a variable content this line ( print "My second text bla bla bla"; ) and when I want to change it I change the content and the previews PHP code will change work when I load the webpage.

I search for solution and some people said use htmlspecialchars() but I don't want to display the line as a text when I load the webpage, BUT to put this line ( print "My second text bla bla bla"; ) really in the PHP code and when I load the webpage it should works as:

<?php
echo "My first text bla bla bla";
print "My second text bla bla bla"; // This line should placed here when I load the webpage
?>


If you don't get the idea please don't down-vote it :) It's an important question :)

Answer

You cant write a php statement within a php statement. As soon as you use echo or print, the code inside echo/print will be rendered as html. The best you could do is store it within a variable. then you can change the variable $var.

$var = "My second text bla bla bla";

echo $var and print $var are basically the same and if you have to change it, change echo to print in the code.