[英]PHP: Whole HTML page as variable - is there a better way?
I have a large form, in the end of the form a user is presented with a summary: 我有一个很大的表单,在表单的末尾,向用户显示了一个摘要:
You have entered:
<table>
<tr>
<td>First name</td>
<td><?php echo $firstname ?></td>
</tr>
<tr>
<td>Last name</td>
<td><?php echo $lastname ?></td>
</tr>
</table>
I first designed the summary page and now the thought came to me that it would be nice to send the user this page as a confirmation e-mail. 我首先设计了摘要页面,但是现在我想到了将此页面作为确认电子邮件发送给用户的想法。 What I have done now is this: 我现在所做的是:
<?php $summarypage = "<table>
<tr>
<td>First name</td>
<td>".$firstname."</td>
</tr>
<tr>
<td>Last name</td>
<td>".$lastname."</td>
</tr>
</table>";
echo $summarypage; ?>
For loops I use $summarypage .= "blabla";
对于循环,我使用$summarypage .= "blabla";
within the loops. 在循环内。
When sending the e-mail I can just take $summarypage
and attach it to my e-mail body. 发送电子邮件时,我可以直接进入$summarypage
并将其附加到我的电子邮件正文中。 Beautiful. 美丽。
Is what I am doing OK? 我在做什么好吗? It seems very "not elegant" to me. 对我来说似乎很“不优雅”。
Isn't $summarypage
being rendered totally anew when I call it again in my e-mail - meaning all variables concatenated with it (eg $firstname
) will be called again - performance hog? 当我在电子邮件中再次调用$summarypage
不是会完全重新呈现它吗-意味着所有与之连接的变量(例如$firstname
)都将再次被调用-性能消耗?
Is there some kind of "buffer" I could just write the $summarypage
variable to, so I have a plain-text variable afterwards? 是否可以将$summarypage
变量写到某种“缓冲区”中,所以之后有一个纯文本变量? Would $newsummarypage = string($summarypage)
do the trick? $newsummarypage = string($summarypage)
会成功吗?
Ignore performance on that level, it won't matter. 忽略该级别的性能,没关系。 If the method you show works for you, use it. 如果您显示的方法适合您,请使用它。
An alternative that makes things a bit more readable (because you won't need PHP openers / closers) is HEREDOC : 另一种使事情更具可读性的替代方法(因为您不需要PHP开启器/关闭器)是HEREDOC :
<?php $summarypage = <<<EOT
<table>
<tr>
<td>First name</td>
<td>$firstname</td>
</tr>
<tr>
<td>Last name</td>
<td>$lastname</td>
</tr>
</table>
EOT;
?>
I think you are a bit confused about how strings/variables work in php. 我认为您对php中的字符串/变量如何工作感到有些困惑。 A little example might help 一个小例子可能会有所帮助
$s = "hello"; //stores the sequence 'h' 'e' 'l' 'l' 'o' in $s
$s = $s." world";//take the sequence stored in $s add the sequence ' world',
//and store in $s again
echo $s; // prints 'hello world'. That is what $s contains, what $s is
$summary = "<div>".$firstname."</div>"; // take '<div>', lookup what $firstname
//contains and add that, then add '</div>' and then store this
//new string thing('<div>Ishtar</div>') in $summary.
echo $summary; //$summary here knows nothing about $firstname,
//does not depend on it
All variables are evaluated when you use them. 使用它们时,将评估所有变量。 (Well, most of the time you use variables by evaluating them.) (嗯,大多数情况下,您通过评估变量来使用变量。)
$summarypage is a string, so it's just a bit of data assigned to a variable - a plain-text variable, as you said. $ summarypage是一个字符串,因此只是分配给变量(如您所说的纯文本变量)的一点数据。 Once the data is assigned to $summarypage, the work is done. 将数据分配给$ summarypage后,工作就完成了。 You can happily write it out into pages, emails, databases and text files with no additional performance hits related to $summarypage. 您可以愉快地将其写到页面,电子邮件,数据库和文本文件中,而不会产生与$ summarypage相关的其他性能影响。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.