HTML 到 PHP 变量(PHP 代码之外的 HTML)
我是 php 的新手,想知道是否可以有这样的东西:
<?php
...
magicFunctionStart();
?>
<html>
<head>...</head>
<body>...</body>
</html>
<?php
$variable = magicFunctionEnd();
...
?>
我现在必须使用的是
<?php
...
$variable = "<html><head>...</head><body>...</body></html>"
?>
Which 很烦人且不可读。
I am new to php and wondering if I can have something like this:
<?php
...
magicFunctionStart();
?>
<html>
<head>...</head>
<body>...</body>
</html>
<?php
$variable = magicFunctionEnd();
...
?>
What I have to use right now is
<?php
...
$variable = "<html><head>...</head><body>...</body></html>"
?>
Which is annoying and not readable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您尝试过“输出缓冲”吗?
Have you tried "output buffering"?
我想你想要 heredoc 语法。
例如:
I think you want heredoc syntax.
For example:
我不太确定你想要完成什么,但我认为类似 heredoc 语法 可能对您有用:
但是,如果您尝试制作 HTML 模板,我强烈建议您使用真正的模板引擎,例如 Smarty,Dwoo< /a> 或 Savant。
I'm not really sure about what you are trying to accomplish, but I think something like the heredoc syntax might be useful for you:
However if you are trying to make HTML templates I would highly recommend you to get a real templating engine, like Smarty, Dwoo or Savant.
好吧,你想做的事情在某种程度上是可能的。
您不能简单地将 HTML 块分配给 php 变量或使用函数来执行此操作。然而,有多种方法可以获得您想要的结果。
您遇到的问题之一是页面中的任何 HTML 都会立即发送到客户端,这意味着它不能用作 php 中的变量。但是,如果您使用这些功能
ob_start和ob_end_fush你可以达到你想要的。
例如,
希望这可以帮助您阅读 php 文档中的 输出缓冲区 。
Ok what you want to do is possible in a fashion.
You cannot simply assign a block of HTML to a php variable or do so with a function. However there is a number of ways to get the result you wish.
One of the problems you have is that any HTML you have in your page is immediately sent to the client which means it cant be used as a variable in php. However if you use the functions
ob_start and ob_end_fush you can achive what you want.
eg
Hope this helps you can read up on output buffers in php docs.
我总是建议避免缓冲函数(如
ob_start
等),只要你有替代方案(因为有时它们可能与相同的部分冲突)系统)。我用:
I always recommend to AVOID buffer functions (like
ob_start
,or etc) whenever you have an alternative (because sometimes they might conflict with parts in same system).I use:
它真的很疯狂,但要注意,如果你这样做:
你会得到它:
保持冷静,它是唯一的 php 试图让你疯狂。
Its REALLY CRAZY but be aware that if you do it :
You will get it:
Keep calm, its only php trying turn you crazy.