是否可以在不使用echo的情况下在PHP中输出类似<h3>Something</h3>
的内容?假设我有一个用于显示数据库信息的类文件,但我希望它是纯PHP,并且没有大的echo语句。我可以这样做,还是需要使用echo
感谢
答案 0 :(得分:1)
尝试关闭并重新打开PHP标记:
# PHP CODE
?>
<h3>Something</h3>
<?php
#MORE PHP CODE
答案 1 :(得分:1)
有很多方法,print()
- die()
- exit()
,HTML,heredoc
和nowdoc
:
<?php
print("<h3>Something</h3>");
和
<?php
die("<h3>Something</h3>");
和
<?php
exit("<h3>Something</h3>");
和好的HTML
<!doctype html>
<head></head>
<body>
<?php
// some code
?>
<h3>Something</h3>
<?php
// some other code
?>
</body>
</html>
修改另外,正如sudo.ie
在回答中所述,使用heredoc。
还有nowdoc可以让你做类似的事情,并从示例#6中获取:
<?php
$str = <<<'EOD'
Example of string
spanning multiple lines
using nowdoc syntax.
EOD;
/* More complex example, with variables. */
class foo
{
public $foo;
public $bar;
function foo()
{
$this->foo = 'Foo';
$this->bar = array('Bar1', 'Bar2', 'Bar3');
}
}
$foo = new foo();
$name = 'MyName';
echo <<<'EOT'
My name is "$name". I am printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
This should not print a capital 'A': \x41
EOT;
?>
答案 2 :(得分:0)
这可能是您正在寻找的,使用heredoc:
<?= <<<EOT
<h1>
This is some PHP text.
It is completely free
I can use "double quotes"
and 'single quotes',
plus $variables too, which will
be properly converted to their values,
you can even type EOT, as long as it
is not alone on a line, like this:
</h1>
EOT;
?>