1 运算符
关于运算符的结合优先级不在这里说明,一般用括号来解决这个问题。
1.1 算术运算符
比较常见,如+ - * / % -等,需要注意的是%运算与分子的符号保持一致。
1.2 赋值运算符
如= += -=
1.3 位运算符
如& | ^ ~ << >>
1.4 比较运算符
$a == $b 二者在类型转换后,值相等
$a === $b 二者类型相同,且值相等
!= <>
!==
< <=
> >=
1.5 错误控制
@expresion 忽略表达式产生的错误信息
1.6 执行运算符
` ` 用于执行shell命令,其等同于shell_exec()
1.7 增减运算符
++
--
支持前、后自增减操作
1.8 逻辑运算符
and == &&
or == ||
xor
not == !
1.9 字符串运算符
. 字符串连接
.= 字符串连接且赋值
1.10 数组运算符
+ 集合并运算
== 集合相同比较运算
=== 集合相同且位置相同比较运算
!= 集合不等比较运算
!== 不满足===比较运算
1.11 类型运算符
$a instanceof Class
2 流程控制
2.1 if...else/else if
<?php
if ($a > $b) {
echo "a is bigger than b";
} elseif ($a == $b) {
echo "a is equal to b";
} else {
echo "a is smaller than b";
}
?>
2.2 while/do...while
<?php
/* example 1 */
$i = 1;
while ($i <= 10) {
echo $i++; /* the printed value would be
$i before the increment
(post-increment) */
}
/* example 2 */
$i = 0;
do {
echo $i;
} while ($i > 0);
?>
2.3 for/foreach
<?php
/*for example 1 */
for ($i = 1; $i <= 10; $i++) {
echo $i;
}
/* foreach example 1: value only */
$a = array(1, 2, 3, 17);
foreach ($a as $v) {
echo "Current value of \$a: $v.\n";
}
/* foreach example 2: value (with its manual access notation printed for illustration) */
$a = array(1, 2, 3, 17);
$i = 0; /* for illustrative purposes only */
foreach ($a as $v) {
echo "\$a[$i] => $v.\n";
$i++;
}
/* foreach example 3: key and value */
$a = array(
"one" => 1,
"two" => 2,
"three" => 3,
"seventeen" => 17
);
foreach ($a as $k => $v) {
echo "\$a[$k] => $v.\n";
}
/* foreach example 4: multi-dimensional arrays */
$a = array();
$a[0][0] = "a";
$a[0][1] = "b";
$a[1][0] = "y";
$a[1][1] = "z";
foreach ($a as $v1) {
foreach ($v1 as $v2) {
echo "$v2\n";
}
}
/* foreach example 5: dynamic arrays */
foreach (array(1, 2, 3, 4, 5) as $v) {
echo "$v\n";
}
2.4 switch
<?php
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
}
?>
2.5 break/continue
略
2.6 declare
指示命令,目前有用的为指定文件编码,如declare(encoding='ISO-8859-1');
2.7 return
从函数中返回
2.8 require/include
用于包含指定的文件,二者作用几乎一样。区别在于处理失败方式不同。前者会返回Fatal,后者只是warning,脚本会继续执行。
require 'prepend.php';
include 'vars.php';