gpt4 book ai didi

php - 不正确的缩进会破坏 php 代码吗?

转载 作者:行者123 更新时间:2023-12-04 00:43:05 25 4
gpt4 key购买 nike

我正在根据用户手册学习 php。现在我正在查看以下代码:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<?php
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;

/* More complex example, with variables. */
class foo
{
var $foo;
var $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 print a capital 'A': \x41
EOT;
?>
</body>
</html>

此代码按预期运行并输出:我的名字是“MyName”。我正在打印一些 Foo。现在,我正在打印一些 Bar2。这应该打印一个大写的“A”:A

当我尝试缩进 php 使其位于 body 标记的前面时,它下面的 HTML 被注释掉,chrome broswer 显示错误:

解析错误:语法错误,第39行C:\xampp\htdocs\xampp\phpnotes\index.php文件意外结束

最佳答案

一般不会。缩进 PHP 代码根本不会影响它。但是,对于 heredoc,此规则有一个异常(exception)。 From the docs :

Warning

It is very important to note that the line with the closing identifier must contain no other characters, except a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system. This is \n on UNIX systems, including Mac OS X. The closing delimiter must also be followed by a newline.

If this rule is broken and the closing identifier is not "clean", it will not be considered a closing identifier, and PHP will continue looking for one. If a proper closing identifier is not found before the end of the current file, a parse error will result at the last line.

简而言之,您的 EOD;EOT; 行必须独立,没有制表符、空格或其他任何内容。

关于php - 不正确的缩进会破坏 php 代码吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27080700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com