PHP流程控制中不常用的替代语法
准备做个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语句的替代语法使用如下:
复制代码代码如下:
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;
?>
版权声明:本文为博主原创文章,未经博主允许不得转载。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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-

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.

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' =>

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

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

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

The Storage::download method of the Laravel framework provides a concise API for safely handling file downloads while managing abstractions of file storage. Here is an example of using Storage::download() in the example controller:

Laravel simplifies HTTP verb handling in incoming requests, streamlining diverse operation management within your applications. The method() and isMethod() methods efficiently identify and validate request types. This feature is crucial for building
