Php 简明教程
PHP - Syntax
PHP 的语法规则与 C 语言非常相似。PHP 是一种服务器端脚本语言。PHP 代码存储为扩展名为“php”的文本文件。PHP 文件本质上是一个网页,内容是穿插在 HTML 脚本中的一个或多个 PHP 代码块。但是,它必须在浏览器中使用 HTTP 协议 URL 打开。也就是说,如果您双击 PHP 文件图标,它将使用文件协议在本地打开。例如,如果您在 Apache 服务器的文档根目录中打开“index.php”文件,它可能只显示 PHP 代码文本。但是,如果您启动 Apache 服务器并打开 URL http://localhost/index.php ,它将显示 Apache 主页。
The syntax rules of PHP are very similar to C Language. PHP is a server side scripting language. A PHP code is stored as a text file with ".php" extension. A ".php" file is essentially a web page with one or more blocks of PHP code interspersed in the HTML script. However, it must be opened in a browser with a HTTP protocol URL. In other words, if you double-click on the PHP file icon, it will be opened locally with the file protocol. For example, if you open the "index.php" file in the document root folder of your Apache server, it may just show the text of the PHP code. However, if you launch the Apache server and open the URL http://localhost/index.php, it displays the Apache home page.
“php”文件可能包含 HTML、CSS 和 JavaScript 代码块以及 PHP 代码。因此,PHP 解析器必须区分出 PHP 代码和其他元素。当在网络浏览器中打开“php”文件时,HTML 引擎会呈现 HTML/CSS/JavaScript 部分,并在遇到包含在 PHP 标记内的语句时即退出 HTML 块。PHP 解析器解释器处理此块并向浏览器返回响应。
A ".php" file may contain HTML, CSS and JavaScript code blocks along with the PHP code. Hence, the PHP parser must differentiate between the PHP code from the other elements. When a ".php" file is opened in the web browser, the HTML engine renders the HTML/CSS/JavaScript part and escapes out of the HTML block as soon as the statements included in PHP tags are encountered. The PHP parser interpreter processes this block and returns the response to the browser.

PHP 定义了使用标记来让 PHP 代码脱离 HTML 的两种方法。标准 PHP 标记和短标记(SGML 样式)。
PHP defines two methods of using tags for escaping the PHP code from HTML. Canonical PHP tags and Short-open (SGML-style) tags.
Canonical PHP Tags
通用最有效的 PHP 标记样式为 −
The most universally effective PHP tag style is −
<?php
One or more PHP statements
?>
如果您使用此样式,则可以确信您的标记将始终被正确解释。
If you use this style, you can be positive that your tags will always be correctly interpreted.
Short-open (SGML-style) Tags
短标记或短开标记看起来像这样 −
Short or short-open tags look like this −
<?php
One or more PHP statements
?>
顾名思义,短标记是最短的选项。您必须执行两件事之一,以使 PHP 能够识别标记 −
Short tags are, as one might expect, the shortest option. You must do one of two things to enable PHP to recognize the tags −
-
Choose the "--enable-short-tags" configuration option when you’re building PHP.
-
Set the "short_open_tag" setting in your php.ini file to on.
short_open_tag=on
必须禁用此选项才能使用 PHP 解析 XML,因为 XML 标记使用相同的语法。
This option must be disabled to parse XML with PHP because the same syntax is used for XML tags.
ASP-style tags −
The use of ASP-style tags −
<%...%>
和 HTML script tags −
and HTML script tags −
<script language = "PHP">...</script>
的使用已被停止。
has been discontinued.
Escaping from HTML
PHP 解析器忽略开闭标记对之外的所有内容。因此,PHP 文件可以具有混合内容。这允许在 HTML 文档中嵌入 PHP −
The PHP parser ignores everything outside of a pair of opening and closing tags. Thus, a PHP file can have mixed content. This allows PHP to be embedded in HTML documents −
<p>This is a HTML statement</p>
<?php echo This is a PHP statement.'; ?>
<p>This is another HTML statement.</p>
下面展示了使用条件脱离的稍高阶示例 −
A little advanced example of escaping using conditions is shown below −
<?php if ($expression == true): ?>
This HTML statement will be rendered.
<?php else: ?>
Otherwise this HTML statement will be rendered.
<?php endif; ?>
PHP 跳过不满足条件的块,即使它们在 PHP 打开/关闭标记之外。
PHP skips the blocks where the condition is not met, even though they are outside of the PHP open/close tags.
对于输出大块文本,退出 PHP 解析模式通常比通过 echo 或 print 发送所有文本更有效率。
For outputting large blocks of text, dropping out of PHP parsing mode is generally more efficient than sending all of the text through echo or print.
Basic Syntax of PHP
PHP 的基本语法与 C 和 C++ 的语法非常相似。
The basic syntax of PHP is very similar to that of C and C++.
Statements are expressions terminated by semicolons
PHP 中的语句是后面紧跟一个分号 (;) 的任何表达式。任何被 PHP 标记括起来的有效 PHP 语句序列都是有效的 PHP 程序。
A statement in PHP is any expression that is followed by a semicolon (;). Any sequence of valid PHP statements that is enclosed by the PHP tags is a valid PHP program.
以下是在 PHP 中一个典型的语句,在这个案例中它给一个叫做 “$greeting” 的变量分配一个字符串 −
Here is a typical statement in PHP, which in this case assigns a string of characters to a variable called "$greeting" −
$greeting = "Welcome to PHP!";
文本编辑器中的一个物理行在 PHP 代码中没有任何意义。一行中可能会包含多个以分号结尾的语句。另一方面,如果需要的话,一个 PHP 语句可以溢出到多行。
A physical line in the text editor doesn’t have any significance in a PHP code. There may be multiple semicolon-terminated statements in a single line. On the other hand, a PHP statement may spill over more than one line if required.
Expressions are combinations of tokens
PHP 最小的构建块是不可分割的标记,诸如数字 (3.14159)、字符串 (“two”)、变量 ($two)、常量 (TRUE) 以及构成 PHP 自身语法的特殊单词,诸如 “if”、“else”、“while”、“for” 等。
The smallest building blocks of PHP are the indivisible tokens such as numbers (3.14159), strings ("two"), variables ($two), constants (TRUE), and the special words that make up the syntax of PHP itself like "if", "else", "while", "for", and so forth.
Braces make blocks
尽管不能将语句像表达式那样组合,但你总可以通过将它们括在一对花括号中,将语句序列放在任何可以放置语句的位置。
Although statements cannot be combined like expressions, you can always put a sequence of statements anywhere a statement can go, by enclosing them in a set of curly braces.
这里,以下两个语句是等价的 −
Here, both the following statements are equivalent −
if (3 == 2 + 1)
print("Good - I haven't totally lost my mind.");
if (3 == 2 + 1) {
print("Good - I haven't totally");
print("lost my mind.");
}
PHP is case sensitive
PHP 是一种大小写敏感的语言。各种 PHP 标识符的名称(诸如变量、函数、类等)都是大小写敏感的。结果是变量 “$age” 不同于 “$Age”。同样,一个名为 “myfunction()” 的函数不同于另一个名为 “MyFunction()” 的函数。
PHP is a case sensitive language. The names of various PHP identifiers such as variable, function, class, etc., are case sensitive. As a result, the variable "$age" is not the same as "$Age". Similarly, a function called "myfunction()" is different from another function called "MyFunction()".