- 实例演示分支与循环
<?php
$username = 'zolo';
$myname = 'zy';
printf("用户名:%s,名字:%s<hr>",$username,$myname);
$uname = &$myname;
printf("u用户名:%s,名字:%s<br>",$uname,$myname);
if($uname == $myname){
printf("u用户名:%s,名字:%s<br>",$uname,$myname);
}else{
echo '不相等<br>';
}
echo $uname == $myname ? '相等<br>': '不相等<br>';
//echo infoSalary(10000);
//echo call_user_func('infoSalary',12345);
class Demo{
public function infoSalary($salary){
return $salary.'<br>';
}
}
class Demo1{
public static function infoSalary($salary){
return $salary.'<br>';
}
}
echo call_user_func([(new Demo),'infoSalary'],'2223332');
echo call_user_func(['Demo1','infoSalary'],'00000009999');
$username = 'zolo1';
$psw = '123123';
if ($username == 'zolo'){
echo '用户名正确'.'<br>';
if($psw == '123123'){
echo '密码正确'.'<br>';
}else{
echo '密码错误'.'<br>';
}
}else{
echo '用户名错误'.'<br>';
}
$age = 15;
switch (true) {
case $age >= 18 && $age < 30:
echo "{$age}岁, 已成年<br>";
break;
case $age >= 30 && $age < 45:
echo "{$age}岁, 老了<br>";
break;
case $age >= 45:
echo "{$age}岁, 太老了 <br>";
break;
default:
echo "{$age}岁, 小屁孩 <br>";
}
$colors = ['red','green','blue','dark'];
$list = '<ul style="border:1px solid;background: pink;display:inline-block">';
for ($i = 0; $i < count($colors); $i++) {
// 只输出前二个
// if ($i > 1) break;
// 跳过第2个,只输出第1个和第3,4个
// if ($i === 1) continue;
// $list .= "<li>{$colors[$i]}</li>";
$list =$list."<li>{$colors[$i]}</li>";
}
echo $list;
echo '</ul><hr>';
- 实例演示php模板语法与html混编技巧
$users = [
0 => ['id' => 1, 'name' => 'ZOLO1', 'gender' => 0, 'age' => 28],
1 => ['id' => 2, 'name' => 'ZOLO2', 'gender' => 1, 'age' => 10],
2 => ['id' => 3, 'name' => 'ZOLO3', 'gender' => 0, 'age' => 35],
];
$table = '<table border="1" width="400" cellspacing="0" cellpadding="3" align="center">
<caption>用户信息表</caption>
<thead bgcolor="#ccc">
<tr>
<th>id</th>
<th>用户名</th>
<th>性别</th>
<th>年龄</th>
</tr>
</thead>
<tbody align="center">';
foreach($users as $user){
// echo print_r($user,true);
//print_r($user);
// 1->true, 0->false
$table .= '<tr>
<td>' . $user['id'] . '</td>
<td>' . $user['name'] . '</td>
<td>' . ($user['gender'] ? '女' : '男') . '</td>
<td>' . $user['age'] . '</td></tr>';
}
$table .= '</tbody></table>';
echo $table;