博客列表 >PHP基础 -(二)分支与循环、php模板语法

PHP基础 -(二)分支与循环、php模板语法

CY明月归
CY明月归原创
2022年04月21日 19:56:29548浏览
  1. 实例演示分支与循环
  1. <?php
  2. $username = 'zolo';
  3. $myname = 'zy';
  4. printf("用户名:%s,名字:%s<hr>",$username,$myname);
  5. $uname = &$myname;
  6. printf("u用户名:%s,名字:%s<br>",$uname,$myname);
  7. if($uname == $myname){
  8. printf("u用户名:%s,名字:%s<br>",$uname,$myname);
  9. }else{
  10. echo '不相等<br>';
  11. }
  12. echo $uname == $myname ? '相等<br>': '不相等<br>';
  13. //echo infoSalary(10000);
  14. //echo call_user_func('infoSalary',12345);
  15. class Demo{
  16. public function infoSalary($salary){
  17. return $salary.'<br>';
  18. }
  19. }
  20. class Demo1{
  21. public static function infoSalary($salary){
  22. return $salary.'<br>';
  23. }
  24. }
  25. echo call_user_func([(new Demo),'infoSalary'],'2223332');
  26. echo call_user_func(['Demo1','infoSalary'],'00000009999');
  27. $username = 'zolo1';
  28. $psw = '123123';
  29. if ($username == 'zolo'){
  30. echo '用户名正确'.'<br>';
  31. if($psw == '123123'){
  32. echo '密码正确'.'<br>';
  33. }else{
  34. echo '密码错误'.'<br>';
  35. }
  36. }else{
  37. echo '用户名错误'.'<br>';
  38. }
  39. $age = 15;
  40. switch (true) {
  41. case $age >= 18 && $age < 30:
  42. echo "{$age}岁, 已成年<br>";
  43. break;
  44. case $age >= 30 && $age < 45:
  45. echo "{$age}岁, 老了<br>";
  46. break;
  47. case $age >= 45:
  48. echo "{$age}岁, 太老了 <br>";
  49. break;
  50. default:
  51. echo "{$age}岁, 小屁孩 <br>";
  52. }
  53. $colors = ['red','green','blue','dark'];
  54. $list = '<ul style="border:1px solid;background: pink;display:inline-block">';
  55. for ($i = 0; $i < count($colors); $i++) {
  56. // 只输出前二个
  57. // if ($i > 1) break;
  58. // 跳过第2个,只输出第1个和第3,4个
  59. // if ($i === 1) continue;
  60. // $list .= "<li>{$colors[$i]}</li>";
  61. $list =$list."<li>{$colors[$i]}</li>";
  62. }
  63. echo $list;
  64. echo '</ul><hr>';
  1. 实例演示php模板语法与html混编技巧
  1. $users = [
  2. 0 => ['id' => 1, 'name' => 'ZOLO1', 'gender' => 0, 'age' => 28],
  3. 1 => ['id' => 2, 'name' => 'ZOLO2', 'gender' => 1, 'age' => 10],
  4. 2 => ['id' => 3, 'name' => 'ZOLO3', 'gender' => 0, 'age' => 35],
  5. ];
  6. $table = '<table border="1" width="400" cellspacing="0" cellpadding="3" align="center">
  7. <caption>用户信息表</caption>
  8. <thead bgcolor="#ccc">
  9. <tr>
  10. <th>id</th>
  11. <th>用户名</th>
  12. <th>性别</th>
  13. <th>年龄</th>
  14. </tr>
  15. </thead>
  16. <tbody align="center">';
  17. foreach($users as $user){
  18. // echo print_r($user,true);
  19. //print_r($user);
  20. // 1->true, 0->false
  21. $table .= '<tr>
  22. <td>' . $user['id'] . '</td>
  23. <td>' . $user['name'] . '</td>
  24. <td>' . ($user['gender'] ? '女' : '男') . '</td>
  25. <td>' . $user['age'] . '</td></tr>';
  26. }
  27. $table .= '</tbody></table>';
  28. echo $table;
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议