奇怪的 PHP 语法

发布于 2024-09-01 01:37:41 字数 246 浏览 13 评论 0原文

我已经在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

隐诗 2024-09-08 01:37:41

这是 PHP 的控制结构的替代语法

您的代码片段相当于:

if(preg_match('/foo.*bar/','foo is a bar')) {
        echo 'success ';
        echo 'foo comes before bar';
}

一般来说:

if(cond):
...
...
endif;

if(cond) {
...
...
}

This is PHP's Alternative syntax for control structures.

Your snippet is equivalent to:

if(preg_match('/foo.*bar/','foo is a bar')) {
        echo 'success ';
        echo 'foo comes before bar';
}

In general:

if(cond):
...
...
endif;

is same as

if(cond) {
...
...
}
情痴 2024-09-08 01:37:41

这种语法风格在嵌入 HTML 时更常用,特别是对于模板/显示逻辑。以这种方式嵌入时,它比花括号语法更容易阅读。

<div>
<? if ($condition): ?>
  <ul>
    <? foreach($foo as $bar): ?>
        <li><?= $bar ?></li>
    <? endforeach ?>
  </ul>
<? endif ?>
</div>

对比:

<div>
<? if ($condition) { ?>
  <ul>
    <? foreach($foo as $bar) { ?>
      <li><?= $bar ?></li>
    <? } ?>
  </ul>
<? } ?>

详细的结束标签使跟踪嵌套代码块变得更容易,尽管这仍然主要取决于个人喜好。

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.

<div>
<? if ($condition): ?>
  <ul>
    <? foreach($foo as $bar): ?>
        <li><?= $bar ?></li>
    <? endforeach ?>
  </ul>
<? endif ?>
</div>

Versus:

<div>
<? if ($condition) { ?>
  <ul>
    <? foreach($foo as $bar) { ?>
      <li><?= $bar ?></li>
    <? } ?>
  </ul>
<? } ?>

The verbose end tags make it a little easier to keep track of nested code blocks, although it's still mostly personal preference.

左耳近心 2024-09-08 01:37:41

http://php.net/manual/en/control-structs。 Alternative-syntax.php

适用于 ifforwhileforeach>切换。混合 PHP 和 HTML 非常方便。

http://php.net/manual/en/control-structures.alternative-syntax.php

Works for if, for, while, foreach, and switch. Can be quite handy for mixing PHP and HTML.

御守 2024-09-08 01:37:41

您可以在控制结构的替代语法 在 PHP 手册中。重新格式化后,您发布的代码如下所示:

if (preg_match('/foo.*bar/','foo is a bar')):
    echo 'success ';
    echo 'foo comes before bar';
endif;

此代码相当于:

if (preg_match('/foo.*bar/','foo is a bar')) {
    echo 'success ';
    echo 'foo comes before bar';
}

此语法也可用于其他几种控制结构。

if ( condition ):
  // your if code
elseif ( other_condition ):
  // optional elseif code
else:
  // optional else code
endif;

while ( condition ):
  // your while code
endwhile;

for ( condition ):
  // your for code
endfor;

foreach ( condition ):
  // your foreach code
endforeach;

switch ( condition ):
  // your switch code
endswitch;

You can read about it in Alternative syntax for control structures in the PHP manual. Reformatted, the code you posted looks like this:

if (preg_match('/foo.*bar/','foo is a bar')):
    echo 'success ';
    echo 'foo comes before bar';
endif;

This code is equivalent to:

if (preg_match('/foo.*bar/','foo is a bar')) {
    echo 'success ';
    echo 'foo comes before bar';
}

This syntax is available for several other control structures as well.

if ( condition ):
  // your if code
elseif ( other_condition ):
  // optional elseif code
else:
  // optional else code
endif;

while ( condition ):
  // your while code
endwhile;

for ( condition ):
  // your for code
endfor;

foreach ( condition ):
  // your foreach code
endforeach;

switch ( condition ):
  // your switch code
endswitch;
许你一世情深 2024-09-08 01:37:41

它相当于:

if(preg_match('/foo.*bar/','foo is a bar')):
 echo 'success ';
 echo 'foo comes before bar';
endif;

相当于:

if(preg_match('/foo.*bar/','foo is a bar')){
    echo 'success ';
    echo 'foo comes before bar';
}

支持非标准条件语法的明智性显然值得怀疑。

It's the equivalent of:

if(preg_match('/foo.*bar/','foo is a bar')):
 echo 'success ';
 echo 'foo comes before bar';
endif;

which is equivalent to:

if(preg_match('/foo.*bar/','foo is a bar')){
    echo 'success ';
    echo 'foo comes before bar';
}

The wisdom of supporting non-standard conditional syntax is obviously questionable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文