[英]Why does this snippet not throw an error?
我遇到了一些我无法解释为什么它不会在PHP中抛出错误的东西。 如果这对你们中的某些人来说非常明显,或者之前已经得到过回答,那就道歉了。
这不会引起任何问题,它只是一种观察和对理解的追求。 我想知道是否有人知道为什么会发生这种情况? 我只是好奇,因为它看起来根本不应该起作用。 我错过了什么?
PHP版本测试:PHP v7.2.12
<?php
class FooBar
{
public function foo()
{
anythingIWantToWrite: // this doesn't throw an error?
return "foo";
}
public function baz()
{
baz: 'foobar'; // this doesn't throw an error?
return "bar";
}
}
$class = new FooBar();
echo $class->foo()."\n";
echo $class->baz();
因为这是有效的goto
语法,即使您实际上并未使用它。 基本上,你的方法可以有一个goto
在其中的语句去anythingIWantToWrite
或baz
。 你没有。
<?php
class FooBar
{
public function foo()
{
goto anythingIWantToWrite;
echo 'I am skipped';
anythingIWantToWrite:
return "foo";
}
public function baz()
{
goto baz;
echo 'I am skipped';
baz: 'foobar'; // 'foobar" is string literal that simply does nothing.
return "bar";
}
}
$class = new FooBar();
echo $class->foo()."\n";
echo $class->baz();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.