1. 运算符
1.1 算数运算符
- $a+$b
- $a-$b
- $a*$b
- $a/$b
- $a%$b
- 取a/b的余数
- $a++
- 先返回$a的值再+1
- ++$a
- $a的值先+1 再返回$a
- $a- -
- 先返回$a的值再-1
- - -$a
- $a的值先-1 再返回$a
- 2**4
- 2的4次方,需PHP5.6以上版本
<?php
$a = 20;
$b = 6;
echo "\$a+\$b=".($a+$b)."<br>";
echo "\$a-\$b=".($a-$b)."<br>";
echo "\$a*\$b=".($a*$b)."<br>";
echo "\$a/\$b=".($a/$b)."<br>";
echo "\$a%\$b=".($a%$b)."<br>";
// 26
// 14
// 120
// 3.3333333333333
// 2
echo "\$a++=".$a++."<br>";
echo "++\$a=".++$a."<br>";
echo "\$a--=".$a--."<br>";
echo "--\$a=".--$a."<br>";
// $a++=20
// ++$a=22
// $a--=22
// --$a=20
echo $a**3;
//8000
1.2 比较运算符
- >
- <
- =
- 赋值运算符
- ==
- 比较值,不比较类型
- ===
- 比较值,也比较类型
var_dump(99=="99");
// bool(true)
var_dump(99==="99");
// bool(false)
var_dump(99===99);
// bool(true)
1.3 太空船运算符
- <=>
- 比较左右两边的数
- 左>右返回1
- 左<右返回-1
- 左=右返回0
var_dump(1<=>99);
var_dump(99<=>1);
var_dump(99<=>99);
// int(-1)
// int(1)
// int(0)
1.4 逻辑运算符
- &&
- and
- ||
- or
- !
- 取反
1.5 三元运算符
- 相当于 true/false ? if true value 1 : if false value 2
- 也可写成 $a=$b?:$c
- PHP7 以后可以写成 $a=$b??$c
$pwd = sha1("abcdefg");
$pwd = isset($pwd)? $pwd:'root';
echo $pwd;
echo "<hr>";
$page = $_GET['p'];
$page = isset($_GET['p'])?$_GET['p']:1;
echo $page;
echo "<hr>";
# 也可写作
$page = $_GET['p']?:1;
echo $page;
echo "<hr>";
#PHP7 以后,可以写作
$page = $_GET['p']??1;
echo $page;
2. 流程控制
2.1 条件分支
2.1.1 单个分支 if
# 1. 单条件分支
if (true) {
echo "hello";
}
# 模板语法简写1:
if (true) echo "hi";
# 模板语法简写2:
if (true):
echo "hi";
endif;
2.1.2 双分支 if…else
<?php
# 2. 双向条件分支
if (true) {
echo "hehe";
} else {
echo "haha";
}
#模板语法
if(false) echo 5;
else echo 6;
if (false) :
echo 1;
else :
echo 2;
endif;
# 可用于混编
if(true):?>
<strong>hehehe<strong/>
<?else:?>
<em>haha</em>
<?endif?>
2.1.3 多分支
2.1.3.1 if…elseif…else
$age = 18;
$gender = "male";
if ($age<20&&$gender==="male") :
echo "boy";
elseif($age>=20&&$gender==="male"):
echo "man";
elseif($age<20&&$gender==="female"):
echo "girl";
elseif($age>=20&&$gender==="female"):
echo "woman";
else:
echo "hehe";
endif;
2.1.3.2 switch
$d = date("w");
switch ((int)$d) {
case '1':
echo "Mon";
break;
case '2':
echo "Tue";
default:
echo "???";
break;
}
#switch 模板语法
switch ((int)$d) :
case '1':
echo "Mon";
break;
case '2':
echo "Tue";
default:
echo "???";
break;
endswitch;
2.2 循环
2.2.1 while
$numbers = [1,2,3,4,5,6,7];
while ($num = current($numbers)) {
echo $num,"<br>";
next($numbers);
}
reset($numbers);
# while 模板语法
while ($num = current($numbers)) :
echo $num,"<br>";
next($numbers);
endwhile;
2.2.2 do…while
reset($numbers);
$num = current($numbers);
do {
echo $num,"<br>";
next($numbers);
} while ($num = current($numbers));
2.2.3 for
for ($i=0; $i < count($numbers); $i++) {
echo $numbers[$i];
}
2.2.4 foreach
foreach($numbers as $key => $value){
echo $numbers[$key]; //=$value
}
2.3 break和continue
2.3.1 break
- 提前结束循环 拦截输出
for ($i=0; $i < count($numbers); $i++) {
if($i>3) break;
echo $numbers[$i];
}
2.3.2 continue
- 跳过一些元素 选择性输出
for ($i=0; $i < count($numbers); $i++) {
if($i==3 || $i==4) continue;
echo $numbers[$i];
}
3. 字符串定义
3.1 单引号,双引号
- ‘’ 单引号不能解析变量,不能解析转义字符如\n,\t,\r
- “” 双引号能解析变量,能解析转义字符如\n,\t,\r(源代码或terminal中可以看出)
- nl2br() 使\n有效显示在浏览器中
echo __DIR__.'\demo1.php';
echo "<pre>1\t2\n3\r4<pre>";
// 1 2
// 3
// 4
$item = "drink";
echo "I would like some $item";
//I would like some drink
echo "I would like some {$item}s";
//I would like some drinks
echo nl2br("I\nam\nsleeping.\n");
// I
// am
// sleeping.
3.2 定界符
- 能解析转义符 \n \t \r
- 适合输出大量,多行,内部有变量的HTML代码
$item = "drink";
$item2 = "burger";
echo <<<EOF
<table style="border:1px solid;">
<tr>
<th>Header</th>
</tr>
<tr>
<td>{$item} 1, {$item2} 1</td>
</tr>
<tr>
<td>{$item} 2, {$item2} 2</td>
</tr>
</table>
EOF;
例
echo <<<eof
<table border="1" cellspacing="0" width=150 height=150 >
<tbody align="center">
<tr>
<td bgcolor="lightblue">1</td>
<td bgcolor="lightgrey">2</td>
<td bgcolor="lightyellow">3</td>
</tr>
<tr>
<td bgcolor="lightgreen">4</td>
<td bgcolor="lightpink">5</td>
<td bgcolor="orange">6</td>
</tr>
<tr>
<td bgcolor="yellow">7</td>
<td bgcolor="green">8</td>
<td bgcolor="bisque">9</td>
</tr>
</tbody>
</table>
eof;
4. require/include区别
- include 后面的代码还可以执行
- require 出现错误后面代码不执行
- 常用于config文件,如数据库连接
5. 简单计算器
<?php
echo <<<eof
<form action="" method="get">
<input type="number" required name="num1">
<select name="op">
<option value="+" selected>+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
<option value="%">%</option>
</select>
<input type="number" required name="num2">
<button type="submit">=</button>
</form>
eof;
$num1 = (int)$_GET["num1"];
$num2 = (int)$_GET["num2"];
$op = $_GET["op"];
$sum;
switch ($op) {
case '+':
$sum = $num1 + $num2;
break;
case '-':
$sum = $num1 - $num2;
break;
case '*':
$sum = $num1 * $num2;
break;
case '/':
if ($num2 == 0) $num2 = null;
else $sum = $num1 / $num2;
break;
case '%':
if ($num2 == 0) $num2 = null;
else $sum = $num1 % $num2;
break;
default:
$sum = $num1 + $num2;
break;
}
echo $num2 == null ? "error at the second number, divisor cannot be zero" : "$num1 $op $num2 = $sum";