将 php 文件加载到变量中时出现问题(加载 php 代码的结果而不是代码作为字符串)
我有一个网站架构,我将内容分配给变量,然后在母版页中打印它们。我的问题是子页面中的 php 代码作为字符串导入到变量中。是否有办法确保代码实际执行并将结果导入到变量中?
在下面的示例中,signup_header.php 中的 php 代码作为字符串导入到 $page_header。结果是“getVerifiedEmail(); ?>”显示在表单元素中而不是电子邮件地址中。
master.phpsignup.php
<!DOCTYPE HTML>
<html>
<head>
<?php echo $page_header; ?>
</head>
<body id="home">
<div class = "container">
<?php echo $page_content; ?>
</div>
</body>
</html>
:
<?php
$page_content = file_get_contents("./include/signup_content.php");
$page_header = file_get_contents("./include/signup_header.php");
include('master.php');
?>
signup_header.phpsignup_content.php
<script type="text/javascript">
$(document).ready(function(){
$('input[name="name"]').attr('value', "<?php echo $idpAssertion->getVerifiedEmail(); ?>");
});
</script>
<section>
<form class="task" method="POST">
Name: <input type="text" name="name" maxlength="30" value=""/><br/>
Email: <input type="text" name="email" value=""/><br/>
UserId: <input id="userId" type="text" name="userId" value="" /><br/>
</form>
</section>
I have a site architechure where I assign content to variables and then print them in a master page. My problem is that php code in the sub pages is imported into the variables as strings. Is there anyway to make sure that the code is actually executed and the results is imported in the variables instead?
In the example below the php code in signup_header.php is imorted as a string to $page_header. The result is that "getVerifiedEmail(); ?>" is displayed in the form element instead of the e-mail address.
master.php
<!DOCTYPE HTML>
<html>
<head>
<?php echo $page_header; ?>
</head>
<body id="home">
<div class = "container">
<?php echo $page_content; ?>
</div>
</body>
</html>
signup.php:
<?php
$page_content = file_get_contents("./include/signup_content.php");
$page_header = file_get_contents("./include/signup_header.php");
include('master.php');
?>
signup_header.php
<script type="text/javascript">
$(document).ready(function(){
$('input[name="name"]').attr('value', "<?php echo $idpAssertion->getVerifiedEmail(); ?>");
});
</script>
signup_content.php
<section>
<form class="task" method="POST">
Name: <input type="text" name="name" maxlength="30" value=""/><br/>
Email: <input type="text" name="email" value=""/><br/>
UserId: <input id="userId" type="text" name="userId" value="" /><br/>
</form>
</section>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用
file_get_contents
文档将返回实际文件的内容。但您希望执行该文件。您可以使用include
文档 执行一个 php 文件,但是大多数情况下该文件本身已经创建了输出。这可能不是您想要的。相反,您仍然可以使用
include
但将输出捕获到缓冲区中。这称为输出缓冲 文档 。为了使您的程序更容易访问此内容,您可以创建一个处理详细信息的小辅助函数。然后,您可以调用该函数来包含相关文件并返回实际输出。然后您可以将返回值分配给变量。
示例:
相关:修改现有 PHP 函数以返回字符串的答案< /a>
Using
file_get_contents
Docs will return the actual file's content. But you're looking to execute the file instead. You can useinclude
Docs to execute a php file, however most often that file will create output itself already. This is probably not what you want.Instead, you can still use
include
but catch the output into a buffer. This is called output-buffering Docs.To make this more accessible for your program, you can create a small helper function that deals with the details. You can then just call that function that will include the file in question and return the actual output. You can then assign the return value to your variables.
Example:
Related: Answer to Modify an Existing PHP Function to Return a String
file_get_contents
返回文件的实际内容,您需要的是 < code>include,实际上是解析一个PHP文件。file_get_contents
returns the actual contents of a file, what you need isinclude
, which actually parses a PHP file.这
就是
我希望的signup_content.php 仅包含类似的模板
and
that's all
I hope that signup_content.php contains the similar template only
在signup.php中使用
这就是你所需要的。
in signup.php use
that is what you need.
使用可以使用eval函数
http://php.net/manual/en/function.eval。 php
use can use eval function
http://php.net/manual/en/function.eval.php