为什么分号在php的最后一条语句中是可选的? [英] Why is the semicolon optional in the last statement in php?

查看:257
本文介绍了为什么分号在php的最后一条语句中是可选的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在编辑器中运行以下代码时,我感到很惊讶:

I was surprised when I ran the following code in my editor:

<?php

    echo "hello";
    echo "world"

?>

很容易看出,代码中缺少了分号(;),但仍然可以使用!

As it can be easily seen, a semicolon (;) is missing from the code, however it still works!

这是如何工作的,为什么这里的;是{0,1}?

How this works and why ; is {0,1} here?.

推荐答案

因为close标记表示分号.您可以在指令分离下的手册中详细了解此内容.

Because the close tag implies a semicolon. You can read more about this in the manual under Instruction separation.

并从那里引用:

与C或Perl中一样,PHP要求在每个语句的末尾以分号终止指令. PHP代码块的结束标记会自动表示一个分号; 您不需要使用分号来终止PHP块的最后一行.该代码块的结束标记将包含紧随其后的换行符(如果存在的话).

As in C or Perl, PHP requires instructions to be terminated with a semicolon at the end of each statement. The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block. The closing tag for the block will include the immediately trailing newline if one is present.

一个证明这一点的例子:

An example to prove this:

1.脚本的末尾缺少分号,但带有结束标记:

1. script with missing semicolon at the end, but with closing tag:

<?php
    echo "1";
    echo "2"
          //^ semicolon missing
?>

输出:

12

2.脚本末尾缺少分号,但没有结束标记:

2. script with missing semicolon at the end, but without closing tag:

<?php
    echo "1";
    echo "2"
          //^ semicolon missing (closing tag missing)

输出:

解析错误:语法错误,文件结尾意外,期望为','或';'在

Parse error: syntax error, unexpected end of file, expecting ',' or ';' in

这篇关于为什么分号在php的最后一条语句中是可选的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆