简体   繁体   English

用PHP编写PHP代码

[英]Write PHP code in PHP

How can I write PHP code in PHP? 如何用PHP编写PHP代码? I want to do this, but it doesn't work: 我想这样做,但是不起作用:

<?php echo '<?php echo \'aoeu\'; ?>'; ?>

Hope someone can 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. 如果您输出一些复杂的代码,则一定要使用诸如smarty之类的模板引擎。否则,您的代码将看起来一团糟。

I once patched the propel ORM which does output PHP code without using a template engine. 我曾经修补过PROM ORM,它无需使用模板引擎即可输出PHP代码。 It generates all the model classes based on the XML configuration files. 它基于XML配置文件生成所有模型类。 Their code was a big mess. 他们的代码一团糟。 Don't do it. 不要这样

<?php echo '&lt;?php echo \'aoeu\'; ?&gt;'; ?>

如果您不转义'<'和'>',它们将被打印为html标签。

Try This 尝试这个

<<< is Heredoc <<<是Heredoc

<?php

echo <<< EOF

<?php
echo <<< EOF
EOF;
?>

EOF;

?>

You can use highlight_string or highlight_file if you use highligh_file you have to indicate php file that you want to show 如果使用highligh_file,则必须使用highlight_string或highlight_file,您必须指出要显示的php文件

<?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"));
?>

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. 我不确定php引擎需要什么(我会用其他张贴者的话说htmlentities可以工作),但是最后使用vim?>会使php代码部分看起来已经结束。

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. 由于许多答案的总结在这里你可以echo Ø print通过改变所有的特殊字符到其对应的HTML特殊代码的PHP和HTML代码。 Al least all ' < ' chars in string should be converted to ' &lt '. 字符串中至少所有' < '字符都应转换为' &lt '。 There are many ways of doing this: 有很多方法可以做到这一点:

echo htmlspecialchars("<b>\"á'");
>> &lt;b&gt;&quot;á'

This converts all characters needed for echoing code. 这将转换回显代码所需的所有字符。

echo htmlentities("<b>\"á'");
>> &lt;b&gt;&quot;&aacute;'

This does the same as htmlspecialchars but also converts all characters that have corresponding html codes. 这和htmlspecialchars一样,但是也转换具有相应html代码的所有字符。

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). 值得注意的是, single double quotesdouble quotes的行为不同,因为single quotes不会解释其中的变量,也不会读取转义字符(单引号本身除外)。

$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 另外,您可以使用heredoc语法,即等于double-quoted字符串,但无需在字符串内转义它们

echo <<<EOF
Your "string' here
EOF;

Or nowdoc syntax wich is the same but imitates a single-quoted string nowdoc语法完全相同,但模仿single-quoted字符串

echo <<<'EOF'
Your "string' here
EOF;

You can read all about these here: https://www.php.net/manual/es/language.types.string.php 您可以在这里阅读所有关于这些的内容: https : //www.php.net/manual/es/language.types.string.php

看看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() . 除非您当然要输出HTML,否则请使用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? 我假设您正在尝试显示<?php ?>标记? If so, just use &lt instead of < 如果是这样,只需使用&lt代替<

This cannot be done directly in the way that you posted. 这不能直接按照您发布的方式进行。

PHP is a server side scripting language as I am sure you know. 我敢肯定,PHP是一种服务器端脚本语言。 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. 执行其他php代码生成的php代码的唯一方法是将新代码写入文件,然后在页面加载后运行该文件。

Example 1 例子1

<?php       echo '<pre>', highlight_string('<?php
include($_SERVER["DOCUMENT_ROOT"] . "/myclass/myform.php");
?>', true), '</pre>';       ?>

Example 2 例子2
See php manual highlight-string PHP手册高亮字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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