2. PHP语法
2.1.1. 大小写敏感
内置的函数和关键字是不区分大小写的,但变量名区分大小写。
2.1.2. 语句和分号
以分号作为语句的结尾。
2.1.3. 空白和空行
为了程序的可读性,以下的方式去空白还是可以的。
//语句raise_prices($inventory, $inflation, $cost_of_living, $greed);可以写成如下:
raise_prices (
$inventory ,
$inflation ,
$cost_of_living ,
$greed
) ;
2.1.4. 注释
PHP 支持 C,C++ 和 Unix Shell 风格(Perl 风格)的注释。例如:
<?php
echo "This is a test"; // This is a one-line c++ style comment
/* This is a multi line comment
yet another line of comment */
echo "This is yet another test";
echo 'One Final Test'; # This is a one-line shell-style comment
?>
单行注释仅仅注释到行末或者当前的 PHP 代码块,视乎哪个首先出现。这意味着在 // ... ?> 或者 # ... ?> 之后的 HTML 代码将被显示出来:?> 跳出了 PHP 模式并返回了 HTML 模式,// 或 # 并不能影响到这一点。如果启用了 asp_tags 配置选项,其行为和 // %> 或 # %> 相同。不过,</script> 标记在单行注释中不会跳出 PHP 模式。
<h1>This is an <?php # echo "simple";?> example.</h1>
<p>The header above will say 'This is an example'.
C 风格的注释在碰到第一个 */ 时结束。要确保不要嵌套 C 风格的注释。试图注释掉一大块代码时很容易出现该错误。
<?php
/*
echo "This is a test"; /* This comment will cause a problem */
*/
?>
2.1.5. 直接量
2001
0xFE
1.4142
"Hello World"
'Hi'
true
null
2.1.6. 标识符
首字母可以使ASCII字母,下划线(_),或ASCII 0x7F到ASCII 0xFF的字符
2.1.6.1 变量名
变量名以$开头,区分大小写。
//非法变量名:
$12 = 12; //使用数字开头的非法变量名
$十二 = 12; //使用其他符号开头的非法变量名
$1two = 12; //使用数字开头的非法变量名
$*12 = 12; //使用其他符号开头的非法变量名
//合法变量名
$age = 12; //合法变量名
$is12 = 12; //以字母开头的合法变量
$_12 = 12; //下划线开头的合法变量名
$_十二 = 12; //下划线开头的合法变量名
$is十二 = 12; //以字母开头的合法变量
2.1.6.2 函数名
不区分大小写。
2.1.6.3 类名
不区分大小写。
2.1.6.4 常量
只有scalar valuesBoolean, integer, double, and stringcan可以被定义为常量。
define('PUBLISHER', "O'Reilly & Associates");
echo PUBLISHER;
2.1.7关键字一览
_ _CLASS_ _ | clone | endif |
_ _FILE_ _ | Const | endswitch |
_ _FUNCTION_ _ | Continue | endwhile |
_ _LINE_ _ | Declare | eval( ) |
_ _METHOD_ _ | Default | exception |
Abstract | die( ) | exit( ) |
And | Do | extends |
array( ) | echo( ) | extends |
As | Else | final |
Break | elseif | for |
Case | empty( ) | foreach |
catch | enddeclare | function |
cfunction | endfor | global |
Class | endforeach | if |
implements | php_user_filter | switch |
include( ) | print( ) | tHRow |
include_once( ) | private | TRy |
interface | protected | unset( ) |
isset( ) | public | use |
list( ) | require( ) | var |
new | require_once( ) | while |
old_function | return( ) | xor |
Or | static |