PHP中的替代文法介紹_php技巧

來源:互聯網
上載者:User

今天看了一下wordpress的代碼,裡面有些少見的php替代文法,

複製代碼 代碼如下:

<?php else : ?>
        <div class="entry-content">
            <?php the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'thebox' ) ); ?>
            <?php wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', 'thebox' ), 'after' => '</div>' ) ); ?>
        </div><!-- .entry-content -->
    <?php endif; ?>

else後面的冒號和endif代表啥?菜鳥一個,沒有見過,所以就google了一下,才明白這是php的替代文法,

冒號(:)等價於左花括弧({),endif等價於右花括弧(});

舉個例子吧:

複製代碼 代碼如下:

<?php if ($a<0): ?>

是負數拉
複製代碼 代碼如下:

<?php endif; ?>

上面的語句等同於
複製代碼 代碼如下:

<?php if ($a<0){ ?>

是負數拉
複製代碼 代碼如下:

<?php } ?>

PHP中那些文法有替代文法?

流程式控制制(包括if,while,forforeach,switch)這幾個語句有替代文法。

替代文法的基本形式:

左花括弧({)換成冒號(:),把右花括弧(})分別換成 endif;,endwhile;,endfor;,endforeach; 以及 endswitch;

while替代文法:

複製代碼 代碼如下:

<?php while (expr): ?>
  <li>迴圈點什麼</li>
<?php endwhile; ?>

其它替代文法可以類推。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.