Blogger Information
Blog 46
fans 2
comment 0
visits 19130
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
1. 实例演示分支与循环, 不要抄源码 2. 实例演示流程控制之模板语法, 自己测试switch
P粉314265155
Original
286 people have browsed it

变量赋值

  1. <?php
  2. // 变量命名与 js规则一样,驼峰 蛇形
  3. namespace _0809;
  4. // 变量赋值
  5. // 1、默认:值传递
  6. $username = '小狗';
  7. printf('$uername = %s <br>',$username);
  8. // 更新
  9. $myName = $username;
  10. printf('$uername = %s | $myName = %s <br>',$username,$myName );
  11. // 2、引用传递
  12. // 变量前 :& 取地址符 表示引用
  13. $yourName = &$username;
  14. $yourName = '小猫';
  15. printf('$uername = %s | $yourName = %s <br>',$username,$yourName );
  16. // 引用传递, 会同步更新原始变量,因为你引用是同一个变量
  17. // "其实引用传递,并没有创建新变量", 而是给原变量,起了一个别名
  18. // 例如,提到川建国, 大家都知道指的是美丽国前总统特朗普,当然,懂王也是它
  19. // 所以, 可以给同一个变量, 起多个别名,是完全可以的, 大家可以试试看

数据类型

  1. <?php
  2. // 数据类型
  3. // 1、基本类型 :不可再分
  4. // 1.1数值 int float
  5. use Demo1 as GlobalDemo1;
  6. use Demo2 as GlobalDemo2;
  7. $age = 20;
  8. $price =80.08;
  9. // 1.2 字符串 string
  10. $username ='小牛';
  11. // 1.3 布尔 boolean
  12. $isDel = true;
  13. // 2、复合类型
  14. // 数组
  15. $arr = [20,'root',true];
  16. // 对象
  17. // 类
  18. // class Demo{}
  19. // $obj = new Demo();
  20. // 匿名类
  21. $obj = new class ( 1000){
  22. public float $salary;
  23. public function __construct(float $salary)
  24. {
  25. $this -> salary = $salary;
  26. }
  27. };
  28. echo gettype($obj);
  29. echo $obj->salary .'<br>';
  30. // 3、null
  31. // 三种情况为null
  32. // 1. 赋值 null, 2. 没赋值 3. 被销毁 unset()
  33. // $a;
  34. // echo gettype($a);
  35. // $b = null;
  36. // echo gettype($b);
  37. // $c = 12;
  38. // unset ($c);
  39. // echo gettype($c);
  40. // 4、 资源类型
  41. // fopen() 打开文件 r 只读 rw 读写 + 追加
  42. $f = fopen('zuoye.php','r');
  43. echo gettype($f) . '<br>';
  44. // callback: 回调
  45. // php用字符串来传递函数,所以可以以任何形式传递函数
  46. // 所以回调函数不仅包括函数 ,还包括对象方法,静态类方法等
  47. // 接受回调的函数很多,例如前面遇到的array_filter
  48. function hello (string $name ,$salary) :string {
  49. return '你好,' . $name .',工资是'.$salary;
  50. }
  51. echo hello('小龙','123') .'<br>';
  52. // echo call_user_func( 函数,参数列表)
  53. echo call_user_func('hello' ,'小蛇',456).'<br>';
  54. $params = ['小鸡',1122];
  55. echo call_user_func_array('hello' ,$params).'<br>';
  56. // 推荐使用 call_user_func_array
  57. /**
  58. * call_user_func(函数名, 参数列表)
  59. * call_user_func_array(函数名, [参数列表...])
  60. * 哪怕只有一个参数,也尽量使用第二种
  61. *
  62. * call_user_func_array()的第一个参数是要执行的函数
  63. * 这个函数参数可以有三个来源
  64. * 1. 用户自定义的函数
  65. * 2. 对象方法
  66. * 3. 类方法/静态方法
  67. */
  68. // 如果这个函数来自对象方法
  69. // 创建Demo1类
  70. class Demo1 {
  71. public function hello ( string $name) : string{
  72. return '你好,' . $name;
  73. }
  74. }
  75. // new 一个实例
  76. $obj =new Demo1;
  77. // hello 不是全局函数 是一个对象方法 固定语法 写到数组
  78. echo call_user_func_array([$obj,'hello'],['小猴']).'<br>';
  79. // 如果这个函数来自: 类方法 static 静态化
  80. // 创建Demo2类
  81. class Demo2 {
  82. public static function hello ( string $name) : string{
  83. return '你好,' . $name;
  84. }
  85. }
  86. // 类方法是用类调用,不需要对象调用
  87. echo call_user_func_array(['Demo2','hello'],['小猪']).'<br>';

流程控制

  1. <?php
  2. namespace _0809;
  3. // 分支 流程控制
  4. // 单分支 if
  5. $age = 19;
  6. if ($age >=18){
  7. echo '已经成年 <br>';
  8. }
  9. // 双分支
  10. $age = 15;
  11. if ($age >=18){
  12. echo '已经成年 <br>';
  13. } else {
  14. echo '未成年 <br>';
  15. }
  16. // 双分支语法糖 三元运算符
  17. // 条件? true :false
  18. $age =19;
  19. echo $age >18 ?'已经成年<br>':'未成年<br>';
  20. // 多分支、
  21. $age =-1;
  22. if ($age >=18 && $age<30){
  23. echo "{$age}岁了,该结婚了<br>";
  24. } elseif ($age >=30 && $age <100){
  25. echo "{$age}岁了<br>";
  26. }
  27. elseif ($age >=100||$age <0){
  28. echo "非法年龄<br>";
  29. }
  30. else {
  31. echo"{$age}岁了,未成年<br>";
  32. }
  33. // $age = 120;
  34. // if ($age >= 18 && $age < 30) {
  35. // echo "{$age}岁,彩礼,能按揭吗?<br>";
  36. // } elseif ($age >= 30 && $age < 45) {
  37. // echo "{$age}岁,应该成个家了<br>";
  38. // } elseif ($age >= 45 && $age < 100) {
  39. // echo "{$age}岁,房贷快还完了<br>";
  40. // } elseif ($age >= 100 || $age <= 6) {
  41. // echo "{$age}是一个非法年龄<br>";
  42. // } else {
  43. // // 默认分支
  44. // echo "{$age}岁,放学,我送你回家<br>";
  45. // }
  46. // 多分支 语法糖 switch
  47. $age = 50 ;
  48. switch (true) {
  49. case $age >18 && $age <60 :
  50. echo "{$age}岁了,不能退休<br>";
  51. break;
  52. case $age >60 :
  53. echo "{$age}岁了,退休了<br>";
  54. break;
  55. default:
  56. echo "非法年龄<br>";
  57. }
  58. /**
  59. * 分支
  60. * 1. 单分支
  61. * 2. 双分支: 三元
  62. * 3. 多分支: switch
  63. */

循环

  1. <?php
  2. namespace _0809;
  3. // 循环
  4. $colors = ['red','green','blue'];
  5. $i =0;
  6. // count() 计算数组长度
  7. echo '数组长度:',count($colors),'<br>';
  8. $list = '<ul style = "border:2px solid">';
  9. if ($i < count($colors)){
  10. $list.="<li>{$colors[$i]}</li>";
  11. }
  12. $i++;
  13. // $i =$i + 1;
  14. // $i+=1;
  15. if ($i < count($colors)) {
  16. $list .= "<li>{$colors[$i]}</li>";
  17. }
  18. $i++;
  19. if ($i < count($colors)) {
  20. $list .= "<li>{$colors[$i]}</li>";
  21. }
  22. // $list = $list.'</ul>';
  23. // 简化
  24. $list .= '</ul>';
  25. echo $list;
  26. /**
  27. * 循环三要素
  28. * 1. $i = 0,初始化
  29. * 2. $i < count($colors), 循环条件
  30. * 3. $i++, 更新循环条件, 否则进入死循环
  31. */
  32. $list = '<ul style = "border:2px solid blue">';
  33. // 1. $i = 0,初始化
  34. $a =0 ;
  35. // 2. $i < count($colors), 循环条件// 入口判断
  36. while ($a < count($colors)){
  37. $list .= "<li>{$colors[$a]}</li>";
  38. // $i++, 更新循环条件
  39. $a++;
  40. };
  41. $list .= '</ul>';
  42. echo $list;
  43. $list = '<ul style = "border:2px solid green">';
  44. // 1. $i = 0,初始化
  45. $b=0;
  46. // 2. $i < count($colors), 循环条件
  47. do {
  48. $list .= "<li>{$colors[$b]}</li>";
  49. // 3. $b++, 更新循环条件
  50. $b=$b + 1;
  51. } while ($b < count($colors));
  52. $list .= '</ul>';
  53. echo $list;
  54. $list = '<ul style = "border:2px solid red">';
  55. for ($c = 0;$c <count($colors);$c++){
  56. $list .= "<li>{$colors[$c]}</li>";
  57. }
  58. $list .= '</ul>';
  59. echo $list;
  60. $list = '<ul style = "border:2px solid red">';
  61. for ($c = 0;$c <count($colors);$c++){
  62. if($c>1) {
  63. // break 跳出循环 。
  64. break;}
  65. $list .= "<li>{$colors[$c]}</li>";
  66. }
  67. $list .= '</ul>';
  68. echo $list;
  69. $list = '<ul style = "border:2px solid blue">';
  70. for ($d = 0;$d<count($colors);$d++){
  71. if($d === 1) {
  72. // 跳过某个循环 。
  73. continue; }
  74. $list .= "<li>{$colors[$d]}</li>";
  75. }
  76. $list .= '</ul>';
  77. echo $list;

数组遍历

  1. <?php
  2. namespace _0809;
  3. // 数组遍历
  4. $colors = ['red','green','blue'];
  5. print_r($colors);
  6. // <pre > 格式化打印
  7. printf('<pre>%s</pre>',$colors);
  8. echo'<hr>';
  9. printf('<pre>%s</pre>',print_r($colors));
  10. echo'<hr>';
  11. printf('<pre>%s</pre>',print_r($colors,true));
  12. echo'<hr>';
  13. // 1、索引数组:键名默认从 0开始的 递增的正整数(数值类型)
  14. // 完整的声明语法
  15. $colors = ['0'=>'red','1'=>'blue','2'=>'green'];
  16. // 用默认的键名可以不写
  17. $colors = ['red','green','blue'];
  18. printf('<pre>%s</pre>',print_r($colors,true));
  19. echo $colors[1];
  20. echo '$colors[1]';
  21. echo '<hr>';
  22. // 2、关联数组 键名是字符串(语义化)
  23. $user = [123,'小王',99];
  24. $user = ['id'=>123,'name'=>'小王','score'=>99];
  25. print $user;
  26. printf('<pre>%s</pre>',print_r($user,true));
  27. // printf('<pre>%s</pre>',print_r($user));
  28. // printf('<pre>%s</pre>',print_r($user[1]));
  29. // printf('<pre>%s</pre>', print_r($user, true));
  30. echo $user['name']. '<br>';
  31. // $arr[$key]
  32. // $key: 数值->索引数组
  33. // $key: 字符串->关联数组
  34. // 数组遍历 :while循环 for循环
  35. // 数组的专用遍历语法 :foreach($arr as $key=>$value){...}
  36. foreach($colors as $value){
  37. echo $value.'<br>';
  38. }
  39. foreach($colors as $key =>$value){
  40. echo $value.'<br>';
  41. printf('[%s]=>%s<br>',$key,$value);
  42. }
  43. echo '<hr>';
  44. foreach($user as $key =>$value){
  45. // echo $value.'<br>';
  46. printf('[%s]=>%s<br>',$key,$value);
  47. }
  48. // 二维数组
  49. // 因为从数据表中的查询结果,php用二维数组来表示
  50. // gender : 0男 1女
  51. $users =[
  52. 0=>['id'=>123,'name'=>'小王','score'=>99,'gender'=>0],
  53. 1=>['id'=>456,'name'=>'小猫','score'=>99,'gender'=>1],
  54. 2=>['id'=>789,'name'=>'小狗','score'=>99,'gender'=>0],
  55. ];
  56. printf('<pre>%s</pre>',print_r($users,true));
  57. // foreach +table 放到渲染到页面上
  58. $table = '<table border="1" width="400" cellspacing="0" cellpadding="3" align="center">';
  59. $table .= '<caption>用户信息表</caption>';
  60. $table .= '<thead bgcolor="#ccc"><tr><th>ID</th><th>用户名</th><th>性别</th><th>成绩</th></tr></thead>';
  61. $table .= '<tbody align="center">';
  62. // 遍历
  63. foreach($users as $user ){
  64. print_r($user);
  65. $table .='<tr>';
  66. $table .="<td>{$user['id']}</td>";
  67. // 简化、 拼接
  68. // $table .='<td>'.$user['id'].'</td>';
  69. $table .="<td>{$user['name']}</td>";
  70. $table .='<td>'.($user['gender']?'女':'男').'</td>';
  71. $table .='<td>'.$user['score'].'</td>';
  72. $table .='</tr>';
  73. }
  74. $table .= '</table>';
  75. // 打印表格到页面中
  76. echo $table;

数据库查询

  1. <!-- 数据库查询
  2. 利用二维数组模拟数据查询结果
  3. -->
  4. <?php
  5. $stus = [
  6. ['id' => 1, 'name' => '刘备', 'course' => 'js', 'score' => 83],
  7. ['id' => 2, 'name' => '关羽', 'course' => 'php', 'score' => 75],
  8. ['id' => 3, 'name' => '张飞', 'course' => 'js', 'score' => 52],
  9. ['id' => 4, 'name' => '孙权', 'course' => 'php', 'score' => 88],
  10. ['id' => 5, 'name' => '周瑜', 'course' => 'js', 'score' => 65],
  11. ['id' => 6, 'name' => '孔明', 'course' => 'php', 'score' => 53],
  12. ['id' => 7, 'name' => '赵云', 'course' => 'js', 'score' => 63],
  13. ['id' => 8, 'name' => '马超', 'course' => 'js', 'score' => 77],
  14. ['id' => 9, 'name' => '姜维', 'course' => 'php', 'score' => 93],
  15. ['id' => 10, 'name' => '黄忠', 'course' => 'js', 'score' => 41],
  16. ]
  17. ?>
  18. <!DOCTYPE html>
  19. <html lang="zh-CN">
  20. <head>
  21. <meta charset="UTF-8">
  22. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  23. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  24. <title>学生信息管理系统</title>
  25. <style>
  26. table {
  27. border-collapse: collapse;
  28. width: 360px;
  29. text-align: center;
  30. }
  31. table th,
  32. table td {
  33. border: 1px solid #000;
  34. padding: 5px;
  35. }
  36. table caption {
  37. font-size: 1.3em;
  38. }
  39. table thead {
  40. background-color: lightcyan;
  41. }
  42. .active {
  43. color: red;
  44. }
  45. </style>
  46. </head>
  47. <body>
  48. <table>
  49. <caption>学生成绩表</caption>
  50. <thead>
  51. <tr>
  52. <th>ID</th>
  53. <th>姓名</th>
  54. <th>课程</th>
  55. <th>成绩</th>
  56. </tr>
  57. </thead>
  58. <tbody>
  59. <?php
  60. foreach($stus as $stu ){
  61. echo '<tr>';
  62. echo "<td>{$stu['id']}</td>";
  63. // 简化、 拼接
  64. // $table .='<td>'.$user['id'].'</td>';
  65. echo "<td>{$stu['course']}</td>";
  66. echo "<td>{$stu['name']}</td>";
  67. echo '<td>'.$stu['score'].'</td>';
  68. echo '</tr>';
  69. }
  70. ?>
  71. </tbody>
  72. <!-- <hr> -->
  73. <tbody>
  74. <?php
  75. foreach($stus as $stu ){?>
  76. <tr>
  77. <td><?php echo $stu['id'] ?></td>
  78. <td><?php echo $stu['course'] ?></td>
  79. <td><?php echo $stu['name'] ?></td>
  80. <td><?php echo $stu['score'] ?></td>
  81. </tr>
  82. <?php }
  83. ?>
  84. </tbody>
  85. <tbody>
  86. <?php
  87. // 将{ 左侧大括号 用: 代替 右侧的大括号用 foreach代替 } 模板语法
  88. // 短标签 将 ?php 用 = 代替 叫短标签
  89. foreach($stus as $stu ) : ?>
  90. <tr>
  91. <td><?= $stu['id'] ?></td>
  92. <td><?=$stu['course'] ?></td>
  93. <td><?php echo $stu['name'] ?></td>
  94. <td><?php echo $stu['score'] ?></td>
  95. </tr>
  96. <?php endforeach
  97. ?>
  98. </tbody>
  99. <tbody>
  100. <?php
  101. // 对成绩进行判断,满足条件输出,不满足条件不输出 数据过滤 替代语法
  102. foreach($stus as $stu ) : ?>
  103. <tr>
  104. <!-- 将 { 用 冒号: 代替 将} 用endif代替 -->
  105. <?php if($stu['score'] >= 70) :?>
  106. <td><?= $stu['id'] ?></td>
  107. <td><?=$stu['course'] ?></td>
  108. <td><?php echo $stu['name'] ?></td>
  109. <td><?php echo $stu['score'] ?></td>
  110. <?php endif ?>
  111. </tr>
  112. <?php endforeach
  113. ?>
  114. </tbody>
  115. <!-- /**
  116. *
  117. * 流程控制的替代语法/模板语法
  118. * 1. foreach - endforeach
  119. * 2. for -> endfor
  120. * 3. if -> endif
  121. * 4. switch -> endswitch
  122. */ -->
  123. </body>
  124. </html>
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:多使用自己例子
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!