PHP:使用多行初始化变量 [英] PHP: Initiate Variable With Multiple Lines

查看:68
本文介绍了PHP:使用多行初始化变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以通过简单地说 $ var = stuff 来创建一个新变量,但是我将如何制作一个这样的变量:

I know I can make a new variable by simply saying $var = "stuff", but how would I make one like this:

<?php
$var = ?>
<html>
<head>
</head>
<body>
</body>
</html>
<? ; ?>

你知道我在做什么吗?有没有一种方法可以不受限制地创建变量并使用 =;

Do you see what I am getting at? Is there a way to create a variable without having to be restrictive and using = "";

感谢您的

推荐答案

听起来像 Heredoc

$var = <<<HTML
<html>
<head>
</head>
<body>
</body>
</html>
HTML;

请注意,结束标记(此处为 HTML; 必须自己一行,没有任何前导或尾随空格(末尾的换行符除外)。

Note, that the end token (here HTML;) must be on a line for itself without any leading or trailing whitespaces (except the newline at the end).

另外,您可以像往常一样将所有内容放入字符串中,但是使用换行符

Additional you can just put everything into a string like usual, but with newlines

$var = '<html>
<head>
</head>
<body>
</body>
</html>';

或自己添加换行符

$var = '<html>' . PHP_EOL;
$var .= '<head>' . PHP_EOL;
$var .= '</head>' . PHP_EOL;
$var .= '<body>' . PHP_EOL;
$var .= '</body>' . PHP_EOL;
$var .= '</html>';

$var = "<html>\n<head>\n</head>\n<body>\n</body>\n</html>";

这篇关于PHP:使用多行初始化变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆