Write PHP code in PHP
How can I write PHP code in PHP? I want to do this, but it doesn't work:
<?php echo '<?php echo \'aoeu\'; ?>'; ?>
Hope someone ca开发者_开发问答n give me a hint,
Many thanks
<?php echo htmlentities('<?php echo \'aoeu\'; ?>'); ?>
If you output some complex code definitely use a template engine like smarty.. otherwise your code will look a complete mess.
I once patched the propel ORM which does output PHP code without using a template engine. It generates all the model classes based on the XML configuration files. Their code was a big mess. Don't do it.
Try This
<<< is Heredoc
<?php
echo <<< EOF
<?php
echo <<< EOF
EOF;
?>
EOF;
?>
<?php echo '<?php echo \'aoeu\'; ?>'; ?>
If you don't escape the '<' and '>' they will be printed as html tags.
You can use highlight_string or highlight_file if you use highligh_file you have to indicate php file that you want to show
<?php
highlight_string('<?php echo\'hello\' ?>');
highlight_file("D:\local\ajax_bottom.php");
?>
or you can combine both which the best solution for me:
<?php
$string=highlight_file("D:\local\hatirlaticilar\Adminn\autocomplete\gethint.php");
highlight_string("$string");
?>
or you can combine in a different way:
<?php
highlight_string(highlight_file("D:\local\hatirlaticilar\Adminn\autocomplete\gethint.php"));
?>
take a look at nowdoc
Escaping the string is all you need to do. You have done it correctly. Unless of course you are outputting for HTML, then use htmlspecialchars().
<?php echo htmlspecialchars('<?php echo \'aoeu\'; ?>'); ?>
http://sandbox.phpcode.eu/g/b1316.php
I am assuming you're trying to show the <?php ?>
tags? If so, just use <
instead of <
This cannot be done directly in the way that you posted.
PHP is a server side scripting language as I am sure you know. This means it is executed before the page is shown to the user.
In effect, your code is writing the text to the browser, but not to the server. The only way to execute php code generated by other php code would be to write the new code to a file, then run the file once the page loads.
Example 1
<?php echo '<pre>', highlight_string('<?php
include($_SERVER["DOCUMENT_ROOT"] . "/myclass/myform.php");
?>', true), '</pre>'; ?>
Example 2
See php manual highlight-string
I'm unsure what the php engine needs (I'll take other poster's words for it that htmlentities works), but using vim the ?> at the end will make it look like the php code section has ended.
Instead I just do this:
string = "<? echo somefunc('strparam'); ?" . ">";
Editor is happy. If there is a better way of doing this, let me know!
echo '<div class="row"><div class="col-md-6 col-md-offset-3"><div class="alert alert-success" role="alert"><p style="text-align: center;">'. php code goes here .'</p></div></div></div>';
As a summary of many answers here you can echo
o print
php and html code by changing all special characters to its correspondent html special codes. Al least all '<' chars in string should be converted to '<'. There are many ways of doing this:
echo htmlspecialchars("<b>\"á'");
>> <b>"á'
This converts all characters needed for echoing code.
echo htmlentities("<b>\"á'");
>> <b>"á'
This does the same as htmlspecialchars
but also converts all characters that have corresponding html codes.
Something worth noting is that single
and double quotes
have a different behavior as single quotes
do not interpret variables inside them nor read escaped characters as such (except the single quote itself).
$foo = "value";
echo "I replace $foo with value and \n with line break";
>> I replace value with value and
with line break
echo 'I don\'t replace $foo with value nor \n with line break';
>> I don't replace $foo with value nor \n with line break
Also you could use heredoc syntax, that is equal as a double-quoted
string but you don't need to escape them inside the string
echo <<<EOF
Your "string' here
EOF;
Or nowdoc syntax wich is the same but imitates a single-quoted
string
echo <<<'EOF'
Your "string' here
EOF;
You can read all about these here: https://www.php.net/manual/es/language.types.string.php
精彩评论