Blogger Information
Blog 23
fans 0
comment 0
visits 18965
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
使用函数封装一个动态表格,自行写5个系统函数
手机用户1617360551
Original
703 people have browsed it

使用函数封装一个动态表格,自行写5个系统函数


1.使用函数封装一个动态表格

  1. <?php
  2. $car = ['品牌','车型','价格','排量'];
  3. $arr = [
  4. [
  5. 'brand' => 'BMW',
  6. 'model' => '330运动曜夜版',
  7. 'price' => '380000元',
  8. 'displacement' =>'2.0T高功'
  9. ],
  10. [
  11. 'brand' => 'BMW',
  12. 'model' => '530豪华版',
  13. 'price' => '480000元',
  14. 'displacement' =>'2.0T高功'
  15. ],
  16. [
  17. 'brand' => 'Audi',
  18. 'model' => 'A8行政版',
  19. 'price' => '980000元',
  20. 'displacement' =>'3.0T高功'
  21. ],
  22. [
  23. 'brand' => 'Audi',
  24. 'model' => 'A4运动版',
  25. 'price' => '280000元',
  26. 'displacement' =>'2.0T低功'
  27. ],
  28. ];
  29. function table(array $car, array $arr,$color){
  30. $table = '<table border="1" width="500px" bgcolor="'.$color.'">';
  31. $table .='<thead>';
  32. $table .= '<tr>';
  33. foreach($car as $value){
  34. $table .='<th>'.$value.'</th>';
  35. }
  36. $table .= '</tr>';
  37. $table .= '</thead>';
  38. $table .= '<tbody>';
  39. foreach($arr as $key1 => $value1){
  40. $table .= '<tr>';
  41. $table .= '<td>'.$value1['brand'].'</td>';
  42. $table .= '<td>'.$value1['model'].'</td>';
  43. $table .= '<td>'.$value1['price'].'</td>';
  44. $table .= '<td>'.$value1['displacement'].'</td>';
  45. $table .= '</tr>';
  46. }
  47. $table .= '</tbody>';
  48. $table .='</table>';
  49. return $table;
  50. }
  51. ?>
  52. <!DOCTYPE html>
  53. <html lang="en">
  54. <head>
  55. <meta charset="UTF-8">
  56. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  57. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  58. <title>Document</title>
  59. </head>
  60. <body>
  61. <?php echo table($car,$arr,'#ccc') ?>
  62. </body>
  63. </html>

代码运行

运行结果

2.自行演示5个字符串系统函数

  1. <?php
  2. // 字符串函数
  3. // 1.ucfirst(string $str): string,将字符串的首字母变成大写
  4. $name = 'peter';
  5. echo ucfirst($name);
  6. echo '<hr>';
  7. // 2.parse_str — 将字符串解析成多个变量
  8. $str = 'name=peter&age=28';
  9. parse_str($str,$str1);
  10. echo $str1['name'].'<br>';
  11. echo $str1['age'];
  12. echo '<hr>';
  13. // 3.str_split 将字符串且切割成数组
  14. $str1 = 'abcdefghigk';
  15. print_r(str_split($str1));
  16. echo "<br>";
  17. $str2 = 'abcdefghit';
  18. //第二个参数,是将字符串取几个字符,拆分到数组中
  19. print_r(str_split($str2,3));
  20. echo '<hr>';
  21. // 4.str_repeat 重复一个字符串,第二个参数是重复的次数,如果是0,
  22. // 则返回一个空数组;
  23. $str3 = '^_^';
  24. echo str_repeat($str3,10);
  25. echo '<hr>';
  26. // 5.strpos 查找字符串首次出现的位置,第二个参数是查询哪个字符
  27. //返回的是第二个参数,首次出现的位置,
  28. $str4 = 'This is a isset';
  29. echo strpos($str4,'is');

演示结果

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