HTML 到 PHP 变量(PHP 代码之外的 HTML)

发布于 2024-08-08 06:00:49 字数 431 浏览 11 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

一抹淡然 2024-08-15 06:00:49

您尝试过“输出缓冲”吗?

<?php
 ...
 ob_start();
?>

<html>
   <head>...</head>
   <body>...<?php echo $another_variable ?></body>
</html>

<?php
 $variable = ob_get_clean();
 ...
?>

Have you tried "output buffering"?

<?php
 ...
 ob_start();
?>

<html>
   <head>...</head>
   <body>...<?php echo $another_variable ?></body>
</html>

<?php
 $variable = ob_get_clean();
 ...
?>
只是偏爱你 2024-08-15 06:00:49

我想你想要 heredoc 语法。

例如:

$var = <<<HTML
<html>
   <head>
random crap here
</html>
HTML;

I think you want heredoc syntax.

For example:

$var = <<<HTML
<html>
   <head>
random crap here
</html>
HTML;
春夜浅 2024-08-15 06:00:49

我不太确定你想要完成什么,但我认为类似 heredoc 语法 可能对您有用:

<?
$variable = <<< MYSTRING

<html>
   <head>...</head>
   <body>...</body>
</html>

MYSTRING;

但是,如果您尝试制作 HTML 模板,我强烈建议您使用真正的模板引擎,例如 SmartyDwoo< /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:

<?
$variable = <<< MYSTRING

<html>
   <head>...</head>
   <body>...</body>
</html>

MYSTRING;

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.

度的依靠╰つ 2024-08-15 06:00:49

好吧,你想做的事情在某种程度上是可能的。

您不能简单地将 HTML 块分配给 php 变量或使用函数来执行此操作。然而,有多种方法可以获得您想要的结果。

  1. 研究模板引擎的使用(我建议您这样做,因为无论如何它都是值得的)。我使用smarty,但还有很多其他
  2. 第二个是使用输出缓冲区。

您遇到的问题之一是页面中的任何 HTML 都会立即发送到客户端,这意味着它不能用作 php 中的变量。但是,如果您使用这些功能
ob_start和ob_end_fush你可以达到你想要的。

例如,

<?php 
  somesetupcode();
  ob_start();  ?>
<html>
<body>
html text
</body>
</html>
<?php
  //This will assign everything that has been output since call to ob_start to your    variable.
  $myHTML = ob_get_contents() ;
  ob_end_flush();

?>

希望这可以帮助您阅读 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.

  1. Investigate the use of a templating engine (I suggest you do this as it is worth while anyway). I use smarty, but there are many others
  2. The second is to use an output buffer.

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

<?php 
  somesetupcode();
  ob_start();  ?>
<html>
<body>
html text
</body>
</html>
<?php
  //This will assign everything that has been output since call to ob_start to your    variable.
  $myHTML = ob_get_contents() ;
  ob_end_flush();

?>

Hope this helps you can read up on output buffers in php docs.

蓝颜夕 2024-08-15 06:00:49

我总是建议避免缓冲函数(如ob_start等),只要你有替代方案(因为有时它们可​​能与相同的部分冲突)系统)。

我用:

function Show_My_Html()
{ ?> 
    <html>
      <head></head>
      <body>
         ...
      </body>
    </html>
    <?php 
}


...
//then you can output anywhere
Show_My_Html();

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:

function Show_My_Html()
{ ?> 
    <html>
      <head></head>
      <body>
         ...
      </body>
    </html>
    <?php 
}


...
//then you can output anywhere
Show_My_Html();
无言温柔 2024-08-15 06:00:49
$html_content = '
    <p class="yourcssclass">Your HTML Code inside apostraphes</p>
';
echo $html_content;
$html_content = '
    <p class="yourcssclass">Your HTML Code inside apostraphes</p>
';
echo $html_content;
隱形的亼 2024-08-15 06:00:49

它真的很疯狂,但要注意,如果你这样做:

<?php echo ""; ?>  

你会得到它:

<html><head></head><body></body></html>  

保持冷静,它是唯一的 php 试图让你疯狂。

Its REALLY CRAZY but be aware that if you do it :

<?php echo ""; ?>  

You will get it:

<html><head></head><body></body></html>  

Keep calm, its only php trying turn you crazy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文