奇怪的 PHP 语法
我已经在 PHP
上工作了一段时间,但是今天当我看到这个时,它对我来说是新的:
if(preg_match('/foo.*bar/','foo is a bar')):
echo 'success ';
echo 'foo comes before bar';
endif;
令我惊讶的是它也运行没有错误。谁能启发我吗?
感谢大家:)
I've been working on PHP
for some time but today when I saw this it came as new to me:
if(preg_match('/foo.*bar/','foo is a bar')):
echo 'success ';
echo 'foo comes before bar';
endif;
To my surprise it also runs without error. Can anyone enlighten me?
Thanks to all :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是 PHP 的控制结构的替代语法。
您的代码片段相当于:
一般来说:
与
This is PHP's Alternative syntax for control structures.
Your snippet is equivalent to:
In general:
is same as
这种语法风格在嵌入 HTML 时更常用,特别是对于模板/显示逻辑。以这种方式嵌入时,它比花括号语法更容易阅读。
对比:
详细的结束标签使跟踪嵌套代码块变得更容易,尽管这仍然主要取决于个人喜好。
That style of syntax is more commonly used when embedding in HTML, especially for template/display logic. When embedded this way, it's a little easier to read than the curly braces syntax.
Versus:
The verbose end tags make it a little easier to keep track of nested code blocks, although it's still mostly personal preference.
http://php.net/manual/en/control-structs。 Alternative-syntax.php
适用于
if
、for
、while
、foreach
和>切换
。混合 PHP 和 HTML 非常方便。http://php.net/manual/en/control-structures.alternative-syntax.php
Works for
if
,for
,while
,foreach
, andswitch
. Can be quite handy for mixing PHP and HTML.您可以在控制结构的替代语法 在 PHP 手册中。重新格式化后,您发布的代码如下所示:
此代码相当于:
此语法也可用于其他几种控制结构。
You can read about it in Alternative syntax for control structures in the PHP manual. Reformatted, the code you posted looks like this:
This code is equivalent to:
This syntax is available for several other control structures as well.
它相当于:
相当于:
支持非标准条件语法的明智性显然值得怀疑。
It's the equivalent of:
which is equivalent to:
The wisdom of supporting non-standard conditional syntax is obviously questionable.