PHP流程式控制制的替代文法

來源:互聯網
上載者:User

標籤:asi   ping   end   site   idg   一個   attribute   開發人員   原因   

PHP流程式控制制的替代文法(The alternative syntax of the PHP process control)早在PHP 4.時代就已存在,只是不見有太多的使用罷了。
##
那麼,PHP中哪些文法有替代文法?

##
針對流程式控制制語句,包括if,while,for,foreach和switch這幾個語句有替代文法。

替代文法的基本形式

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

應用

典型情況下,在純PHP代碼中一般不使用上述替代文法形式,以便與其他流行語句盡量保持一致的可讀性原因所致吧。但是,在網頁語言中,當你使用PHP開發時,就會經常見到流程語句的替代文法形式了。
也就是說,這些文法能發揮的地方是在PHP和HTML混合頁面的代碼裡面。好處如下:

1.使HTML和PHP混合頁面代碼更加乾淨整齊。

有代碼潔癖的朋友最懼怕的就是亂七八糟的混合代碼,有了這些沒有花括弧的替代文法,各位愛乾淨的朋友開心到尿震。

2.流程式控制制邏輯更清晰,代碼更容易閱讀

要改別人的PHP和HTML混合代碼,開啟發現,我擦!太TMD垃圾了!如果用替代文法,我想再垃圾的程式開發人員也不至於寫的太亂吧。

3.一些從ASP等其他類basic語言家族轉來的朋友,會更容易使用PHP。

##
一個典型的例子是在WordPress網站代碼中,你會經常見到。舉例如下:

<?php/** * The template for displaying the header * * Displays all of the head element and everything up until the "site-content" div. * * @package WordPress * @subpackage Twenty_Fifteen * @since Twenty Fifteen 1.0 */?><!DOCTYPE html><html <?php language_attributes(); ?> class="no-js"><head>    <meta charset="<?php bloginfo( ‘charset‘ ); ?>">    <meta name="viewport" content="width=device-width">    <link rel="profile" href="http://gmpg.org/xfn/11">    <link rel="pingback" href="<?php bloginfo( ‘pingback_url‘ ); ?>">    <!--[if lt IE 9]>    <script src="<?php echo esc_url( get_template_directory_uri() ); ?>/js/html5.js"></script>    <![endif]-->    <?php wp_head(); ?></head><body <?php body_class(); ?>><div id="page" class="hfeed site">    <a class="skip-link screen-reader-text" href="#content"><?php _e( ‘Skip to content‘, ‘twentyfifteen‘ ); ?></a>    <div id="sidebar" class="sidebar">        <header id="masthead" class="site-header" role="banner">            <div class="site-branding">                <?php                    twentyfifteen_the_custom_logo();                    if ( is_front_page() && is_home() ) : ?>                        <h1 class="site-title"><a href="<?php echo esc_url( home_url( ‘/‘ ) ); ?>" rel="home"><?php bloginfo( ‘name‘ ); ?></a></h1>                    <?php else : ?>                        <p class="site-title"><a href="<?php echo esc_url( home_url( ‘/‘ ) ); ?>" rel="home"><?php bloginfo( ‘name‘ ); ?></a></p>                    <?php endif;                    $description = get_bloginfo( ‘description‘, ‘display‘ );                    if ( $description || is_customize_preview() ) : ?>                        <p class="site-description"><?php echo $description; ?></p>                    <?php endif;                ?>                <button class="secondary-toggle"><?php _e( ‘Menu and widgets‘, ‘twentyfifteen‘ ); ?></button>            </div><!-- .site-branding -->        </header><!-- .site-header -->        <?php get_sidebar(); ?>    </div><!-- .sidebar -->    <div id="content" class="site-content">
上述代碼來自於著名的Twenty_Fifteen主題中的header.php檔案定義。

PHP流程式控制制的替代文法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.