最近在玩wordpress,发现其主题中的模板文件中经常出现一些带有冒号的“怪”语法。第一感觉这是 wordpress 本身定义的,类似于smarty这样的标签语法,从而需要先将其翻译成纯PHP代码,然后才能执行。很快发现我的想法是错误的!这种特殊语法与WordPress无关,而是PHP语言本身的特性!而且PHP手册中就有说明,只是本人孤陋寡闻了,实在是惭愧。今天把官文说明记录到这里,以备后用。

(1)基本规则,用:代替开始的{  ,用endif;等代替关闭的}。

(2)注意:这种替代语法不能和常规语法混用。

(3)这种替代语法的唯一目的是为了把PHP嵌入到html中时,使得代码整洁明了。

PHP offers an alternative syntax for some of its control structures; namely, if, while, for, foreach, and switch. In each case, the basic form of the alternate syntax is to change the opening brace to a colon (:) and the closing brace to endif;, endwhile;, endfor;, endforeach;, or endswitch;, respectively. <?php if ($a == 5): ?>
A is equal to 5
<?php endif; ?>  In the above example, the HTML block "A is equal to 5" is nested within an if statement written in the alternative syntax. The HTML block would be displayed only if $a is equal to 5. The alternative syntax applies to else and elseif as well. The following is an if structure with elseif and else in the alternative format: <?php
if ($a == 5):echo "a equals 5";echo "...";
elseif ($a == 6):echo "a equals 6";echo "!!!";
else:echo "a is neither 5 nor 6";
endif;
?>  Note: Mixing syntaxes in the same control block is not supported. Warning
Any output (including whitespace) between a switch statement and the first case will result in a syntax error. For example, this is invalid: <?php switch ($foo): ?><?php case 1: ?>...
<?php endswitch ?>  Whereas this is valid, as the trailing newline after the switch statement is considered part of the closing ?> and hence nothing is output between the switch and case: <?php switch ($foo): ?>
<?php case 1: ?>...
<?php endswitch ?> 

你知道PHP的另一种语法吗,我孤陋寡闻了相关推荐

  1. iOS获取自1970年毫秒数使用OC和swift两种语法,返回字符串

    iOS获取自1970年毫秒数使用OC和swift两种语法,返回字符串 oc的话,我们定义一个category分类,对谁分类,对NSDate分类,对外暴露一个类方法,只要导入头文件,即可使用. @int ...

  2. python基础语法—布尔值/布尔运算/循环四种语法

    布尔值: 布尔值(True,False):可以直接再while循环中使用,为真继续执行,为假停止(首字母大写) False:False, 0 ,''(空字符串)[](空列表){}(空字典) None( ...

  3. java 8 语法糖_Java8的lamada算是一种语法糖吗?

    语法糖是啥?语法糖没有一个官方的定义(或者说定义相当宽泛),更像是一种观念:任何让你写着爽.但是实际发生的和你看到的不一样的东西都可以"被认为"是语法糖.例如最常见的Integer ...

  4. mysql 分页查询几种语法_各数据库2种分页语法支持

    Mycat后端通过jdbc接入oracle.db2.sqlserver.postgresql等通过以下2种方式支持分页语法 以防找不到可以先收藏哦! 1 数据库原生分页直接解析 应用端可以通过直接执行 ...

  5. python有多少种语法_这20个常规Python语法你都搞明白了吗?

    Python简单易学,但又博大精深.许多人号称精通Python,却不会写Pythonic的代码,对很多常用包的使用也并不熟悉.学海无涯,我们先来了解一些Python中最基本的内容. Python的特点 ...

  6. C++中for循环的5种语法

    #include <algorithm>   #include <vector>   int nArray[] = {0, 1, 2, 3, 4, 5};   std::vec ...

  7. js中的for循环如何跳出,js中for循环的两种语法

    js几种for循环的几种用法 谷歌人工智能写作项目:小发猫 js,for循环是怎么运行的? typescript有哪些变化. 最普遍的介绍:for循环是JavaScript中最常用的循环,标准for循 ...

  8. LINQ语句的两种语法实现方式

    using System; using System.Linq;namespace LINQ语法实现 {class Program{static void Main(string[] args){in ...

  9. C++ 主函数几种语法

    C++main函数的两种写法 1,返回值为int,且main不带参数 #include<iostream> using namespace std; int main() { cout & ...

最新文章

  1. 2018-3-20李宏毅机器学习笔记十----------Logistic Regression
  2. 【常见CPU架构对比】维基百科
  3. couchdb 视图操作_couchDB视图
  4. gj11 多线程、多进程和线程池编程
  5. Oracle事务的隔离
  6. linux上如何设置ssh免密登陆
  7. SuperMap GIS查询
  8. CMD 命令行查看端口被哪个程序占用,并根据PID值,找到相应的程序,关闭掉对应服务或进程!...
  9. sqlserver 指定的网络名不再可用_50个比较实用的SQL Server查询语句(1)
  10. LeetCode(589)——N叉树的前序遍历(JavaScript)
  11. 在Java中使用Rserve调用R
  12. java文件对比工具
  13. 成语接龙 | c++ | 建图和BFS
  14. DNS域名解析常用命令
  15. 计算机二级vbf资源百度云,2018年计算机二级考试考前冲刺卷
  16. latex制作幻灯片模板
  17. bootstrap实现导航栏的响应式布局,当在小屏幕、手机屏幕浏览时自动折叠隐藏
  18. 实战开发:新手小白如何用C++制作经典网游DNF
  19. 信号强度诊断(系统架构)
  20. Designing Websites for Performance 如何设计高性能网站 Lynda课程中文字幕

热门文章

  1. 什么是Webshell?
  2. 假设有两名囚徒a和b python_囚徒问题(100 prisoners problem)的python验证
  3. verilog符号 与或非 异或_异或应用实例(verilog)
  4. oracle redact数据编译后 查询报错 不是selected表达式
  5. excel的日期和时间
  6. ffserver详细介绍
  7. Manjaro deepin 睡眠后无法唤醒
  8. java实现这个无表头空表格,《java程序导出excel表格是空白的没有数据?》 java怎么导入数据库...
  9. 区块链蓝皮书《中国区块链发展报告(2019)》发布 Conflux作为底层技术代表被收录...
  10. 《SQL数据分析——从基础破冰到面试题解》题解1