Include dynamic PHP in generated php file
I am trying to create php files that include a html template file like so:
$page = htmlspecialchars("Hello! <?php include("template.html"); ?>");
But when I run it I get:
Parse error: syntax error, unexpected T_STRING in /home/oliver/web/postmanapp.com/public/core.php on line 15
I'm pretty sure I need to avoid the PHP code, I just don't know how.
a source to share
You need to avoid any double quotes inside double quoted strings. So this should work:
$code = "Hello! <?php include(\"template.html\"); ?>";
See the PHP Manual about strings .
And if you want to put this code in a file, you can use the function file_put_contents
:
file_put_contents('myscript.php', $code);
a source to share
The answer is a combination of some of the other suggestions, but none of them solve the problem, not even the combined one!
Problem 1. Unescaped double quotes
"Hello! <?php include("template.html"); ?>"
Here you have what looks like one line. PHP sees the open quote and then gets the quote right before the word pattern and it says, oh, now the string is being executed. Then he sees a pattern, which he thinks is a variable. Then he sees the point and considers it to be the new concatenation operator. Then he sees the quote mark after html
and he considers it to be a newline, which is not allowed immediately after such a variable.
So, you need:
'Hello! <?php include("template.html"); ?>'
(This is what I would recommend. Single quotes in PHP are slightly faster because you don't have to search inside a string for variables.)
or, as another person suggested, using the escape character for double quotes:
"Hello! <?php include(\"template.html\"); ?>"
Problem 2. Wrong place for htmlspecialchars ()
But you have another, worse problem. htmlspecialchars basically turns all HTML characters into their HTML character entities.
In other words, it becomes . <
<
So, what you output to your PHP file would be:
Hello! <?php include("template.html"); ?>
When you run this file, PHP will not run because there is no valid open tag.
Problem 3. It's not a good use of include
I am betting on a better way to solve your problem than using PHP to write a PHP include tag containing an ANOTHER file.
An early user is right ... file_put_contents () or file_get_contents () is probably something closer to what you want. But actually, anytime you use PHP to write to another PHP file, there is probably a better way. I almost guarantee it.
a source to share
The problem is? > which always ends a block of code in php, resulting in an inexhaustible line. All you have to do is split the end of the tag into two separate lines that can be concatenated:
$page = htmlspecialchars("Hello! <?php include("template.html"); ?" . ">");
And as the previous answer said, you also need to avoid double quotes inside the string, or use single quotes:
$page = htmlspecialchars("Hello! <?php include('template.html'); ?" . ">");
I also recommend getting in the habit of using single quotes whenever you are not using overridden variable substitution. I seem to remember reading this a little faster since the string doesn't need to parse the variable names.
a source to share