search
HomeBackend DevelopmentPHP TutorialPHP流程控制中不常用的替代语法

准备做个wordpress的主题,结果看到了如下的语法:

    <div id="primary" class="content-area">        <main id="main" class="site-main" role="main">        <?php if ( have_posts() ) : ?>            <?php if ( is_home() && ! is_front_page() ) : ?>                <header>                    <h1 class="page-title screen-reader-text"><?php single_post_title(); ?></h1>                </header>            <?php endif; ?>            <?php // Start the loop.            while ( have_posts() ) : the_post();                /*                 * Include the Post-Format-specific template for the content.                 * If you want to override this in a child theme, then include a file                 * called content-___.php (where ___ is the Post Format name) and that will be used instead.                 */                get_template_part( 'content', get_post_format() );            // End the loop.            endwhile;            // Previous/next page navigation.            the_posts_pagination( array(                'prev_text'          => __( 'Previous page', 'twentyfifteen' ),                'next_text'          => __( 'Next page', 'twentyfifteen' ),                'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentyfifteen' ) . ' </span>',            ) );        // If no content, include the "No posts found" template.        else :            get_template_part( 'content', 'none' );        endif;        ?>        </main><!-- .site-main -->    </div><!-- .content-area -->



这才知道php是有不太常用的替代语法的: 据 http://www.jb51.net/article/23252.htm

PHP中那些语法有替代语法? 
流程控制(包括if,while,forforeach,switch)这几个语句有替代语法。 

替代语法的基本形式: 
左花括号({)换成冒号(:),把右花括号(})分别换成 endif;,endwhile;,endfor;,endforeach; 以及 endswitch; 

举个例子吧: 

复制代码代码如下:


 
是负数拉 
 
上面的语句等同于 
 
是负数拉 
 



为什么纯PHP代码里面几乎看不到?  
这些语法有点不符合C家族的传统有点另类,大家不大习惯于这种语法,而且不是很方便 

大家都不习惯,而且这么另类要他有什么用?蛋疼吗? 
存在就是合理,它自有它的用处,这些语法能发挥的地方是在PHP和HTML混合页面的代码里面。好处如下: 
1.使HTML和PHP混合页面代码更加干净整齐。 
有代码洁癖的朋友最惧怕的就是乱七八糟的混合代码,有了这些没有花括号的替代语法,各位爱干净的朋友开心到尿震。 
2.流程控制逻辑更清晰,代码更容易阅读 
要改别人的PHP和HTML混合代码,打开发现,我擦!太TMD垃圾了!如果用替代语法,我想再垃圾的程序开发人员也不至于写的太乱吧。 
3.一些从ASP等其他类basic语言家族转来的朋友,会更容易使用PHP。 

讲半天没用的,捞点干的行不?这东西怎么用? 

根据之前描述的使用方法,if语句的替代语法使用如下: 

复制代码代码如下:


 

等于5
 
 
等于5
 
 
不是5就是6
 
 

while替代语法:  

复制代码代码如下:


 

  • 循环点什么
  •  
     

    for替代语法:  

    复制代码代码如下:


     

  • 循环点什么
  •  
     

    foreach替代语法: 

    复制代码代码如下:


     

  • 循环点什么
  •  
     


    switch替代语法: 

    复制代码代码如下:


    switch ($i): 
    case 0: 
    echo "i equals 0"; 
    break; 
    case 1: 
    echo "i equals 1"; 
    break; 
    case 2: 
    echo "i equals 2"; 
    break; 
    default: 
    echo "i is not equal to 0, 1 or 2"; 
    endswitch; 
    ?> 

    版权声明:本文为博主原创文章,未经博主允许不得转载。

    Statement
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

    Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

    cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

    The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

    Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

    Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

    PHP Logging: Best Practices for PHP Log AnalysisPHP Logging: Best Practices for PHP Log AnalysisMar 10, 2025 pm 02:32 PM

    PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

    12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

    Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

    Explain the concept of late static binding in PHP.Explain the concept of late static binding in PHP.Mar 21, 2025 pm 01:33 PM

    Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

    Customizing/Extending Frameworks: How to add custom functionality.Customizing/Extending Frameworks: How to add custom functionality.Mar 28, 2025 pm 05:12 PM

    The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    WebStorm Mac version

    WebStorm Mac version

    Useful JavaScript development tools

    SAP NetWeaver Server Adapter for Eclipse

    SAP NetWeaver Server Adapter for Eclipse

    Integrate Eclipse with SAP NetWeaver application server.

    MantisBT

    MantisBT

    Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Dreamweaver Mac version

    Dreamweaver Mac version

    Visual web development tools