奇怪的PHP语法 [英] Strange PHP syntax

查看:339
本文介绍了奇怪的PHP语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 PHP 上工作了一段时间,但今天我看到它时对我来说很新:

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?

感谢所有:)

推荐答案

嵌入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.

<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.

这篇关于奇怪的PHP语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆