如何使用在php文件中分配的变量加载phtml文件

如何使用在php文件中分配的变量加载phtml文件,html,php,email-templates,Html,Php,Email Templates,我正在尝试从一个php文件中的phtml加载或获取所有内容 除了phtml文件中的变量没有加载值外,一切都正常 这是我的php文件 <?php $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $code = rand(11111,99999); $emailSubject = 'Email Verification'; $userEmail = 'some@some.com'; function load

我正在尝试从一个php文件中的phtml加载或获取所有内容

除了phtml文件中的变量没有加载值外,一切都正常

这是我的php文件

<?php
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$code = rand(11111,99999);
$emailSubject = 'Email Verification';
$userEmail = 'some@some.com';

function loadEmailTemplate($pagelink='') {

$page = 'email-templates/' . $pagelink . '.phtml';
$pageContent = '';

ob_start();
include($page);
$pageContent = ob_get_contents();
ob_end_clean();

return $pageContent;
}

$mailBody = loadEmailTemplate('emailtemplate');

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: no-reply-some@some.com\r\n"."X-Mailer: php";
mail($userEmail, $emailSubject, $mailBody, $headers);
?>
这意味着phtml文件正在加载,但我想加载php文件中定义的变量值,我以为我们是在定义php变量后加载phtml文件的内容,该变量将自动分配变量值,但事实并非如此,我错了。能不能请一些人帮我找到解决这个问题的办法


谢谢

您需要告诉Apache处理那些文件,比如
php

加:


SetHandler应用程序/x-httpd-php
到您的apache配置文件或
.htaccess
文件


有关更多信息,请阅读:

要解决问题,您只需在phtml文件中添加全局定义

以下是您的phtml的外观

<?php global $firstname, $lastname; ?>
<html>
<body>
<div style="background-color: #bb4e4e; width:80%; color: #fff;">
 Thank you for registering <?php echo $firstname; ?>, your verification code is <?php echo $code; ?>.
</div>
</body>
</html>

感谢您的注册,您的验证码为。

仍然没有加载值如果您没有使用
require\u once
(或类似),它们尚未定义(不同的HTTP请求、不同的上下文)。尝试将它们传递给会话:
$\u session['firstname']=$firstname
在您的第一个文件中,并在phtmlIn中回显,为了使用会话变量,您还需要在phtml中启动会话:
如果(!session_id())session\u start()您的会话想法有效,谢谢。虽然我没有在htaccess中添加该代码,但phtml正在工作
 Thank you for registering <?php echo $firstname; ?>, your verification code is <?php echo $code; ?>.
Thank you for registering John, your verification code is 52256.
<FilesMatch "\.ph(p[2-6]?|tml)$">
   SetHandler application/x-httpd-php
</FilesMatch>
<?php global $firstname, $lastname; ?>
<html>
<body>
<div style="background-color: #bb4e4e; width:80%; color: #fff;">
 Thank you for registering <?php echo $firstname; ?>, your verification code is <?php echo $code; ?>.
</div>
</body>
</html>