Table of Contents
PHP流程控制的替代语法示例
Home php教程 php手册 PHP流程控制的替代语法示例

PHP流程控制的替代语法示例

Jun 13, 2016 am 09:23 AM
php wordpress learn control substitute process of Example grammar

PHP流程控制的替代语法示例

 了解过wordpress一类博客程序的模板的PHPer都会看到很多奇怪的PHP语法,比如:

空的

对于一部分PHPer根本没见过这些是什么东西呢?难道是那些博客的开发者自己搞的仿PHP的模板语言?
非也,其实这些都是PHP的语法,只不过不常用而已,这些都是PHP流程控制的替代语法。
这里就就给大家详细说一下PHP流程控制的替代语法。什么是替代语法?
简单的说就是一些语法的另类写法。
PHP中那些语法有替代语法?
流程控制(包括if,while,foreach,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 of this Website
    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

    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

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Recommended product registration plug-in for easy-to-use wordpress Recommended product registration plug-in for easy-to-use wordpress Apr 20, 2025 am 08:15 AM

    There is no perfect WordPress product registration plugin, the choice should be based on actual needs and website size. Recommended plug-ins include: MemberPress: powerful but high-priced, complex configuration Restrict Content Pro: Focus on content restrictions and member management, cost-effective Easy Digital Downloads: Sell digital products, and users register as additional functions

    How to display wordpress comments How to display wordpress comments Apr 20, 2025 pm 12:06 PM

    Enable comments in WordPress website: 1. Log in to the admin panel, go to "Settings" - "Discussions", and check "Allow comments"; 2. Select a location to display comments; 3. Customize comments; 4. Manage comments, approve, reject or delete; 5. Use <?php comments_template(); ?> tags to display comments; 6. Enable nested comments; 7. Adjust comment shape; 8. Use plugins and verification codes to prevent spam comments; 9. Encourage users to use Gravatar avatar; 10. Create comments to refer to

    WordPress website is online but cannot be searched WordPress website is online but cannot be searched Apr 20, 2025 am 09:00 AM

    Reasons why WordPress websites cannot be found in search engines: 1. Indexing issues; 2. Content issues; 3. Website technical issues; 4. Link issues; 5. Other issues such as geographical restrictions, website name and social media presence.

    How to close comments with wordpress How to close comments with wordpress Apr 20, 2025 am 11:54 AM

    How to turn off a comment in WordPress? Specific article or page: Uncheck Allow comments under Discussion in the editor. Whole website: Uncheck "Allow comments" in "Settings" -> "Discussion". Using plug-ins: Install plug-ins such as Disable Comments to disable comments. Edit the topic file: Remove the comment form by editing the comments.php file. Custom code: Use the add_filter() function to disable comments.

    What to do if there is an error in wordpress What to do if there is an error in wordpress Apr 20, 2025 am 11:57 AM

    WordPress Error Resolution Guide: 500 Internal Server Error: Disable the plug-in or check the server error log. 404 Page not found: Check permalink and make sure the page link is correct. White Screen of Death: Increase the server PHP memory limit. Database connection error: Check the database server status and WordPress configuration. Other tips: enable debug mode, check error logs, and seek support. Prevent errors: regularly update WordPress, install only necessary plugins, regularly back up your website, and optimize website performance.

    WordPress website building and avoid pits WordPress website building and avoid pits Apr 20, 2025 am 08:06 AM

    Be cautious when building a WordPress website. The guide to breaking through pits helps you avoid risks: choose paid themes and avoid the quality and safety risks of free themes. "Less is more" when installing plugins to avoid website speed and compatibility issues. Regularly optimize the database to ensure the smooth operation of the website. Pay attention to security measures and regularly update and install security plug-ins. Modify the code carefully to avoid website crashes and do it in a test environment if necessary. Pay attention to performance optimization, improve website speed, and improve user experience.

    How to make a video website for wordpress How to make a video website for wordpress Apr 20, 2025 am 08:42 AM

    WordPress is competent for video websites, but you should be cautious. The tools required include paid video plug-ins, suitable themes, cloud storage services, and video processing services. Optimizing performance, choosing the right format, paying attention to SEO and ensuring security are critical.

    WordPress website account login WordPress website account login Apr 20, 2025 am 09:06 AM

    To log in to a WordPress website account: Visit the login page: Enter the website URL plus "/wp-login.php". Enter your username and password. Click "Login". Verification Two-step Verification (optional). After successfully logging in, you will see the website dashboard.

    See all articles