语法概述
这一章将给你介绍下基本的 PHP 语法,对增强你的 PHP 的基础非常重要。
php 解析
PHP 解析引擎需要一种方法来区分其他页面中的 PHP 代码。这样的机制被称为 PHP 解析。有四个方式可以去解析:
典范的 PHP 标签
最普遍有效的 PHP 标记风格是:
1 |
<span class="php"><span class="hljs-preprocessor"><?php</span>...<span class="hljs-preprocessor">?></span></span> |
如果你使用这种风格,可以确保你的标签将被正确的解析。
短标签形式(SGML 形式)标签
短标签如下所示:
1 |
<span class="php"><span class="hljs-preprocessor"><?</span>...<span class="hljs-preprocessor">?></span></span> |
短标签,正如你所预料的那样,最短的选项。你必须做两件事中的一件,使 PHP 识别标签:
- 当你构建 PHP 时,选择
--enable-short-tags
配置选项。 - 在 php.ini 文件设置
short_open_tag
为 on。PHP 配置文件中必须禁用此选项防止解析 XML,因为相同的语法适用于 XML 标记。
ASP 风格的标签
Asp 风格标签被用在动态服务器页面定义代码块。Asp 风格标记看起来像这样:
1 |
<span class="hljs-tag"><<span class="hljs-title">%...%</span>></span> |
使用 Asp 风格标签,您将需要在 php 中设置 php.ini 文件中的配置选项。
HTML 脚本标签
HTML 脚本标签如下所示:
1 |
<span class="hljs-tag"><<span class="hljs-title">script</span> <span class="hljs-attribute">language</span>=<span class="hljs-value">"PHP"</span>></span><span class="undefined">...</span><span class="hljs-tag"></<span class="hljs-title">script</span>></span> |
PHP 代码的注释
注释是程序的一部分,为了在程序执行显示结果之前帮助读者解读代码。在 PHP 中有两种注释:
单行注释:他们通常用于对相关的本地代码做简短解释或笔记。这是单行注释的例子。
1 2 3 4 5 6 7 8 9 10 11 |
<span class="php"><span class="hljs-preprocessor"><?</span> <span class="hljs-comment"># This is a comment, and</span> <span class="hljs-comment"># This is the second line of the comment</span> <span class="hljs-comment">// This is a comment too. Each style comments only</span> <span class="hljs-keyword">print</span> <span class="hljs-string">"An example with single line comments"</span>; <span class="hljs-preprocessor">?></span></span> |
多行注释:下面的例子用一个 print 语句来打印多行:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<? <span class="hljs-comment"># First Example</span> <span class="hljs-built_in">print</span> <<<END This uses the <span class="hljs-string">"here document"</span> syntax to output multiple lines <span class="hljs-reserved">with</span> $variable interpolation. Note that the here <span class="hljs-built_in">document</span> terminator must appear <span class="hljs-literal">on</span> a line <span class="hljs-reserved">with</span> just a semicolon <span class="hljs-literal">no</span> extra whitespace! END; <span class="hljs-comment"># Second Example</span> <span class="hljs-built_in">print</span> <span class="hljs-string">"This spans multiple lines. The newlines will be output as well"</span>; ?> |
多行注释:他们通常在必要时用来为代码提供算法和更详细的解释。多行注释风格和在 C 语言中是一样的,下面有多行注释的例子。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<span class="php"><span class="hljs-preprocessor"><?</span> <span class="hljs-comment">/* This is a comment with multiline Author : Mohammad Mohtashim Purpose: Multiline Comments Demo Subject: PHP */</span> <span class="hljs-keyword">print</span> <span class="hljs-string">"An example with multi line comments"</span>; <span class="hljs-preprocessor">?></span></span> |
PHP 对空格不敏感
空格在你输入的屏幕上通常是看不见的,包括空格、制表符、回车(行尾字符)。
PHP 对空格不敏感,这意味着在 PHP 一行中你有多少空白字符对你没有影响。一个空格就相当于一个字符一样。
例如,以下任何一个 PHP 语句把 2+2 的总和赋给变量 $four
求值是相等的。
1 2 3 4 5 6 7 8 9 |
<span class="hljs-variable">$four</span> = <span class="hljs-number">2</span> + <span class="hljs-number">2</span>; <span class="hljs-regexp">//</span> single spaces <span class="hljs-variable">$four</span> <tab> = <tab2<tab><tab> + <tab><span class="hljs-number">2</span> ; <span class="hljs-regexp">//</span> spaces <span class="hljs-keyword">and</span> tabs <span class="hljs-variable">$four</span> = <span class="hljs-number">2</span>+ <span class="hljs-number">2</span>; <span class="hljs-regexp">//</span> multiple lines |
PHP 是大小写敏感的
是的,没错,PHP 是一种大小写敏感的语言。试试下面的例子:
1 2 3 4 5 6 7 8 9 |
<span class="hljs-tag"><<span class="hljs-title">html</span>></span> <span class="hljs-tag"><<span class="hljs-title">body</span>></span> <span class="php"><span class="hljs-preprocessor"><?</span> <span class="hljs-variable">$capital</span> = <span class="hljs-number">67</span>; <span class="hljs-keyword">print</span>(<span class="hljs-string">"Variable capital is $capital<br>"</span>); <span class="hljs-keyword">print</span>(<span class="hljs-string">"Variable CaPiTaL is $CaPiTaL<br>"</span>); <span class="hljs-preprocessor">?></span></span> <span class="hljs-tag"></<span class="hljs-title">body</span>></span> <span class="hljs-tag"></<span class="hljs-title">html</span>></span> |
这将会输出以下内容:
1 2 |
Variable capital <span class="hljs-keyword">is</span> <span class="hljs-number">67</span> Variable CaPiTaL <span class="hljs-keyword">is</span> |
语句表达式由分号终止
PHP 的任何表达式语句后面的是一个分号(;)。任何在 PHP 标签里有效的 PHP 语句都是有效的 PHP 程序,下面的语句在PHP 中是一个典型的语句,在这种情况下将字符串赋值给一个名为 $greeting
的变量:
1 |
<span class="hljs-variable">$greeting</span> = <span class="hljs-string">"Welcome to PHP!"</span>; |
表达式的组合令牌
最小的 PHP 的构建模块是不可分割的令牌,如数字(3.14159),字符串(.two.),变量($two),常量(TRUE)和特殊的单词构成的 PHP 语法本身,像 if, else, while, for and so forth。
花括号组成代码块
虽然语句不能像表达式那样相结合,你可以在任何地方把一系列语句包含在一组花括号内。
下面这两个语句是等价的:
1 2 3 4 5 6 7 8 |
<span class="hljs-keyword">if</span> (<span class="hljs-number">3</span> == <span class="hljs-number">2</span> + <span class="hljs-number">1</span>) <span class="hljs-keyword">print</span>(<span class="hljs-string">"Good - I haven't totally lost my mind.<br>"</span>); <span class="hljs-keyword">if</span> (<span class="hljs-number">3</span> == <span class="hljs-number">2</span> + <span class="hljs-number">1</span>) { <span class="hljs-keyword">print</span>(<span class="hljs-string">"Good - I haven't totally"</span>); <span class="hljs-keyword">print</span>(<span class="hljs-string">"lost my mind.<br>"</span>); } |
在命令提示符中运行 PHP 脚本
是的,你可以在你的命令提示符里运行 PHP 脚本。假设您已经有下面的 test.php 文件
1 2 3 |
<span class="php"><span class="hljs-preprocessor"><?php</span> <span class="hljs-keyword">echo</span> <span class="hljs-string">"Hello PHP!!!!!"</span>; <span class="hljs-preprocessor">?></span></span> |
现在在命令提示符里运行这个脚本如下:
1 |
<span class="hljs-variable">$ </span>php test.php |
脚本将会输出以下内容:
1 |
<span class="hljs-title">Hello</span> PHP!!!!! |
希望你现在具有 PHP 语法的基本知识。