Blogger Information
Blog 28
fans 0
comment 0
visits 16937
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
12月02日 - PHP基础知识复习
SmileHoHo
Original
558 people have browsed it

PHP基础知识复习

代码区

  1. <?php
  2. //数据,变量表示,放在数据段
  3. $site = '北京';
  4. //代码,函数表示,放在代码段
  5. function get_site($website){
  6. return $website . '欢迎您!';
  7. }
  8. //调用执行。
  9. echo get_site($site);
  10. echo '<hr>';
  11. //变量名是name,变量值的类型是字符串
  12. $name = 'Miss Zhu';
  13. $age = 18;
  14. $isMarried = true;
  15. //单值读取 echo:回显,无返回值
  16. echo '名字:'. $name . '<br>年龄:' . $age . '<br>婚否:' . $isMarried;
  17. echo '<hr>';
  18. //字符串虽是单值,也可以像数组一样访问
  19. echo '第五个字母是:' . $name{5};
  20. echo '<hr>';
  21. //print: 打印, 与echo功能类似 , 但是会有返回值: 1
  22. print('年龄:' . $age . '<br>');
  23. echo print('年龄:' . $age . '<br>');
  24. echo '<hr>';
  25. // print_r($var, false|true)
  26. print_r($name);
  27. echo '<hr>';
  28. echo print_r($name, true);
  29. echo '<hr>';
  30. //从1-20,间隔3
  31. $data = range(1,20,3);
  32. echo '<pre>'.print_r($data,true).'</pre>';
  33. echo '<hr>';
  34. var_export($name);
  35. echo '<hr>';
  36. var_dump($name, $age, $isMarried);

  1. <?php
  2. //索引数组
  3. //1. 定义 直接定义
  4. $user[] = 101;
  5. $user[] = 'admin';
  6. $user[] = 'admin@qq.com';
  7. //推荐方式
  8. $user = [101,'admin','admin@qq.com'];
  9. //2.访问: 单个或多个成员
  10. echo $user[2]. '<br>';
  11. print_r($user);
  12. echo '<pre>'.print_r($user,true).'</pre>';
  13. var_dump($user);
  14. echo '<hr>';
  15. //3. 遍历: 可循环访问全部成员
  16. //for: 索引数组,最常用的是for循环
  17. $res = '';
  18. for($i = 0;$i < count($user); $i++) {
  19. $res .=$user[$i] .' , ';
  20. }
  21. // 去掉最后的', ', 输出遍历结果
  22. echo rtrim($res, ', ');
  23. echo '<hr>';
  24. //foreach
  25. $res = '';
  26. foreach ($user as $v){
  27. $res .= $v.', ';
  28. }
  29. echo rtrim($res,', ');
  30. echo '<hr>';
  31. //转换
  32. list($id, $name, $email) = $user;
  33. echo $id . '-' . $name . '-' . $email . '<hr>';
  34. $str = implode(',', $user);
  35. echo $str, '<br>';
  36. $sql = "INSERT `users` SET `comment` = {$str};";
  37. echo '<hr>';
  38. $arr = explode(',', $str);
  39. print_r($arr);
  40. echo '<hr>';
  41. while (list($key, $value) = each($user)) {
  42. echo '[' . $key . '] => ' . $value . '<br>';
  43. }
  44. echo '<hr>';
  45. echo '<hr>';
  46. // 关联数组
  47. $user = ['id'=>101, 'name'=>'admin', 'email'=>'admin@php.cn'];
  48. // 访问
  49. echo $user['email'] . '<br>';
  50. print_r($user);
  51. var_dump($user);
  52. var_export($user);
  53. echo '<hr>';
  54. //遍历
  55. //foreach: 推荐
  56. foreach ($user as $key=>$value) {
  57. echo '[' . $key . '] => ' . $value . '<br>';
  58. }
  59. //for: 并不适合关联数组,但是借助数组指针, 也可以完成遍历
  60. $res = '';
  61. for ($i = 0; $i < count($user); $i++) {
  62. $res .= current($user) . ', ';
  63. next($user);
  64. }
  65. echo rtrim($res, ', ');
  66. echo '<hr>';
  67. //转换
  68. extract($user);
  69. echo 'id = ' . $id . ', name = ' . $name . ', email = ' . $email . '<br>';
  70. $dsn = 'mysql:host=localhost;dbname=demo';
  71. $username = 'root';
  72. $password = 'root';
  73. $linkParams = compact('dsn', 'username', 'password');
  74. print_r($linkParams);
  75. echo '<br>';
  76. // 关联数组转索引数组
  77. print_r(array_values($user));



手抄作业:








Correcting teacher:天蓬老师天蓬老师

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