单独使用分组语句是否有效?
Is it valid to use grouped statements on their own?
我不认为这是有效的 PHP(我确定我几年前尝试过一次),但令我惊讶的是 运行(虽然我只测试过PHP 5.5.9):
<?php
$arr = [];
$arr[] = 'Hi.';
{
$arr[] = '> Hi there.';
$arr[] = '> What can I do for you?';
}
$arr[] = 'Nothing really.';
print_r($arr);
输出...
Array
(
[0] => Hi.
[1] => > Hi there.
[2] => > What can I do for you?
[3] => Nothing really.
)
PHP 手册在 introducing control structures 时将 {...}
称为组语句 - 但是,我找不到在该上下文之外使用它们的任何提及。它确实说 "A statement-group is a statement by itself as well",但这似乎有点含糊。
这是真的吗?如果我像这样使用大括号,我可以期望我的代码在未来的版本中 运行 吗?如果一个语句组本身被认为是一个语句,我是否应该期望它会影响异常堆栈跟踪中的行号和帧?
Is this genuinely valid?
是的,因为至少 4.3
If I use braces like this, can I expect my code to run in future versions?
直到它从语言中删除,是的。重大更改很少发生。
If a statement group is considered a statement by itself, should I expect it to have an affect line numbers and frames in exception stack traces?
框架不包含 if
、while
和 for
等组语句,因此我认为:不。为什么不试试呢?
我不认为这是有效的 PHP(我确定我几年前尝试过一次),但令我惊讶的是 运行(虽然我只测试过PHP 5.5.9):
<?php
$arr = [];
$arr[] = 'Hi.';
{
$arr[] = '> Hi there.';
$arr[] = '> What can I do for you?';
}
$arr[] = 'Nothing really.';
print_r($arr);
输出...
Array
(
[0] => Hi.
[1] => > Hi there.
[2] => > What can I do for you?
[3] => Nothing really.
)
PHP 手册在 introducing control structures 时将 {...}
称为组语句 - 但是,我找不到在该上下文之外使用它们的任何提及。它确实说 "A statement-group is a statement by itself as well",但这似乎有点含糊。
这是真的吗?如果我像这样使用大括号,我可以期望我的代码在未来的版本中 运行 吗?如果一个语句组本身被认为是一个语句,我是否应该期望它会影响异常堆栈跟踪中的行号和帧?
Is this genuinely valid?
是的,因为至少 4.3
If I use braces like this, can I expect my code to run in future versions?
直到它从语言中删除,是的。重大更改很少发生。
If a statement group is considered a statement by itself, should I expect it to have an affect line numbers and frames in exception stack traces?
框架不包含 if
、while
和 for
等组语句,因此我认为:不。为什么不试试呢?