Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用eval执行php和html代码_Php_Html - Fatal编程技术网

如何使用eval执行php和html代码

如何使用eval执行php和html代码,php,html,Php,Html,如何使用eval函数同时执行html和php eval ("<center> this is some html </center>"); eval(“这是一些html”); 此代码不起作用,我可能必须将其更改为 echo ("<center> this is some html </center>"); echo(“这是一些html”); 但有时候,我需要做一些事情,比如 eval("<center> hi my name i

如何使用eval函数同时执行html和php

eval ("<center> this is some html </center>");
eval(“这是一些html”);
此代码不起作用,我可能必须将其更改为

echo ("<center> this is some html </center>");
echo(“这是一些html”);
但有时候,我需要做一些事情,比如

eval("<center> hi my name is <?php echo \$name; ?> </center>");
eval(“你好,我的名字是”);
这似乎有点奇怪,但我有一个案例,我应该使用eval

$name = 'test';
$string = '<center> hi my name is $name </center>';
eval("\$string = \"$string\";");
echo $string;
$name='test';
$string='嗨,我的名字是$name';
eval(“\$string=\“$string\”;”);
echo$字符串;

要在eval()函数中使用html,请在传递给eval()的字符串中添加前缀

这样,

eval ("?> <center> this is some html </center> <?php");

eval(“?>这是一些html嗨,我的名字是你根本不执行html;html不是可执行代码。在我编写或维护的许多PHP项目中,我从来没有遇到过使用
eval
的好理由。我同意,html不是可执行代码,但我在问如何解决这个问题并做类似
eval的事情(“嗨,我的名字是”);
,当然eval从来都不是一个好的解决方案,但在我正在处理的项目中,我有一个包含html和php的字符串,这个字符串应该作为eval函数的参数…现在正确的答案告诉我如何做…假设这是我的字符串
$code=“嗨,我的名字是“;
我所要做的就是
eval(“?>”$code.”我理解这个问题。这仍然不是使用
eval
的好理由。这是实现真正的模板系统(使用WordPress样式的快捷码、Smarty或类似的东西)的好理由但所有反对
eval
的论点都适用于模板系统,可能比大多数情况下更适用。验证非常困难,使用
eval
通常会掩盖潜在的严重风险。
eval("?> <center> hi my name is <?php echo \$name; ?> </center> <?php");