博客列表 >0107-常量,引号,数组,分支

0107-常量,引号,数组,分支


原创
2020年01月08日 02:20:32830浏览

常量的创建与访问

常量由defineconst创建
不同处类用const,define报错
define允许使用表达式,const只能用字面量

PHP

  1. <?php
  2. // 常量
  3. //define()
  4. echo 'define创建 <br>';
  5. define('herName', 'LP');
  6. echo herName . '<br>' ;
  7. echo '<hr>';
  8. //const
  9. echo 'const创建 <br>';
  10. const herAd = 'GuangZhou';
  11. echo herAd . '<br>';
  12. echo '<hr>';
  13. // 不同处
  14. echo '不同处类用const,define报错 <br>';
  15. class Test
  16. {
  17. //类用const,define报错
  18. const Test = 'classTest';
  19. }
  20. echo Test::Test, '<br>';
  21. echo '<hr>';
  22. //define允许使用表达式,const只能用字面量
  23. echo 'define允许使用表达式,const只能用字面量 <br>';
  24. $domain = 'Test2';
  25. define('mian_Test', $domain);
  26. echo 'TTTT' . mian_Test . '<br>';

效果图

双引号对变量和特殊字符的处理方式

单引号不能识别变量并输出,双引号可以
\可解析符号

PHP

  1. <?php
  2. //单引号双引号
  3. $name = 'amdin';
  4. // 单引号
  5. echo '单引号 <br>';
  6. echo '$name\t <br>';
  7. echo '<hr>';
  8. // 双引号
  9. echo '双引号 <br>';
  10. echo "$name\t <br>";
  11. echo '<hr>';
  12. // \原样输出
  13. echo '\原样输出 <br>';
  14. echo "\$name\t <br>";
  15. echo '<hr>';
  16. // 应用自身
  17. echo '应用自身 <br>';
  18. echo ' \'T\'<br>';
  19. echo "\"T\"<br>";
  20. echo "\\T\\<br>";
  21. echo '<hr>';
  22. //解析链接一起
  23. echo '解析链接一起 <br>';
  24. echo "T${name}iioi<br>";

索引数组与关联数组的常用操作

  1. <?php
  2. // 索引数组
  3. $usersl[0] = 10;
  4. $usersl[1] = 'admin';
  5. $usersl[2] = 'test';
  6. echo '索引数组加索引: <br>';
  7. $str = print_r($usersl, true);
  8. echo "<pre>" . $str . "</pre>";
  9. echo '<hr>';
  10. //不加索引
  11. echo '索引数组不加索引: <br>';
  12. $users2[] = 10;
  13. $users2[] = 'admin';
  14. $users2[] = 'test';
  15. $users2[] = 'test11';
  16. $str = print_r($users2, true);
  17. echo "<pre>$str</pre>";
  18. echo '<hr>';
  19. //添加元素
  20. echo '索引数组字面量添加元素: <br>';
  21. $users3 = [20, 'shuju', 'yuanu'];
  22. $str = print_r($users3, true);
  23. echo "<pre>$str</pre>";
  24. echo '<hr>';
  25. //查看数组更多信息
  26. echo '索引数组查看数组更多信息: <br>';
  27. var_dump($usersl, $users2, $users3);
  28. echo '<hr>';
  29. //取一个
  30. echo '索引数组取值: <br>';
  31. echo $users2[2], '<br>';
  32. echo '<hr>';
  33. //遍历
  34. echo '索引数组循环遍历: <br>';
  35. foreach ($users3 as $value) {
  36. echo $value, '<br>';
  37. }
  38. echo '<hr>';
  39. //数组元素与变量转换
  40. echo '索引数组元素与变量转换: <br>';
  41. list($age, $name, $email) = $users3;
  42. echo "age=>$age,name=$name,email=>$email <br>";
  43. echo '<hr>';
  44. //将索引转为字符串
  45. echo '索引数组索引转为字符串: <br>';
  46. $str = implode('===', $users3);
  47. echo gettype($str), ':', $str, '<br>';
  48. $sql = "INSERT `user` set `comment` = ${str}";
  49. echo '<hr>';
  50. //将字符串转成索引数组
  51. echo '索引数组字符串转成索引数组: <br>';
  52. $str = 'html,css,js,php';
  53. $arr = explode(',', $str);
  54. echo "<pre>" . print_r($arr, true) . "</pre>";
  55. echo '<hr>';
  56. // 关联数组
  57. // 语义化键名
  58. echo '关联数组语义化键名: <br>';
  59. $products['id'] = 998;
  60. $products['name'] = '电脑';
  61. $products['price'] = 666;
  62. echo "<pre>" . print_r($products, true) . "</pre>";
  63. echo '<hr>';
  64. //字面量创建
  65. echo '关联数组字面量创建: <br>';
  66. $products1 = ['id' => 998, 'name' => '相机', 'peice' => 666];
  67. echo "<pre>" . print_r($products1, true) . "</pre>";
  68. echo '<hr>';
  69. //数组遍历
  70. echo '关联数组遍历: <br>';
  71. foreach ($products1 as $value) {
  72. echo $value, '<br>';
  73. }
  74. // key取键名
  75. echo 'key';
  76. foreach ($products1 as $key => $value) {
  77. echo "[$key]=>$value <br>";
  78. }
  79. echo '<hr>';
  80. // 数组转变量
  81. echo '关联数组数组转变量: <br>';
  82. extract($products1);
  83. echo "id=>$id,name=>$name,peice=>$peice";
  84. echo '<hr>';
  85. // 变量转数组
  86. echo '关联数组变量转数组: <br>';
  87. $lpp1 = 'yiduanwenzi';
  88. $lpp2 = 'yiduanwenzi';
  89. $lpp3 = 'yiduanwenzi';
  90. $test = compact('lpp1', 'lpp2', 'lpp3');
  91. echo "<pre>" . print_r($test, true) . "</pre>";
  92. echo '<hr>';
  93. // 关联数组转索引数组
  94. echo '关联数组转索引数组: <br>';
  95. $index = array_values($products1);
  96. //之前
  97. echo '之前: <br>';
  98. echo "<pre>" . print_r($products1, true) . "</pre>";
  99. //转换
  100. echo '转换: <br>';
  101. echo "<pre>" . print_r($index, true) . "</pre>";
  102. echo '<hr>';
  103. // 多维数组
  104. $users = [
  105. ['id' => 1, 'name' => 'name1', 'grade' => 4],
  106. ['id' => 2, 'name' => 'name2', 'grade' => 5],
  107. ['id' => 3, 'name' => 'name3', 'grade' => 6],
  108. ];
  109. // echo "<pre>".print_r($users,true)."</pre>";
  110. // 访问
  111. echo '多维数组访问: <br>';
  112. echo $users[1]['name'], '<br>';
  113. echo '<hr>';
  114. //遍历
  115. echo '多维数组遍历<br>';
  116. foreach ($users as $user) {
  117. echo "id=>${user['id']}, name=>${user['name']}, price=>${user['grade']} <br>";
  118. }

效果图

分支控制的所有形式的基本实现

PHP

  1. <?php
  2. //分支
  3. // 单分支
  4. echo "单分支";
  5. $count = 100;
  6. $price = 500;
  7. $total = 0;
  8. $discount = 1;
  9. if ($count >= 100 && $count < 200) {
  10. $discount = 0.6;
  11. }
  12. $total = $count * $price * $discount;
  13. echo "数量: $count, 总金额: $total, 折扣: $discount <hr>";
  14. //双分支
  15. echo "双分支";
  16. $count = 199;
  17. // $count = 12;
  18. if ($count >= 188 && $count < 200) {
  19. $discount = 0.6;
  20. } else {
  21. $discount = 1;
  22. }
  23. $total = $count * $price * $discount;
  24. echo "购买数量: $count, 总金额: $total, 折扣率: $discount <hr>";
  25. //多分支
  26. echo "多分支";
  27. $count = 50;
  28. $count = 105;
  29. $count = 251;
  30. $count = 357;
  31. if ($count >= 100 && $count < 200) {
  32. $discount = 0.7;
  33. } elseif ($count >= 250 && $count < 300) {
  34. $discount = 0.6;
  35. } elseif ($count >= 300) {
  36. $discount = 0.5;
  37. } else {
  38. $discount = 1;
  39. }
  40. $total = $count * $price * $discount;
  41. echo "购买数量: $count, 总金额: $total, 折扣率: $discount <hr>";
  42. //switch 简化
  43. echo "switch 简化";
  44. $count = 18;
  45. $count = 168;
  46. $count = 248;
  47. $count = 368;
  48. switch (true) {
  49. case $count >= 100 && $count < 200:
  50. $discount = 0.7;
  51. break;
  52. case $count >= 200 && $count < 300:
  53. $discount = 0.6;
  54. break;
  55. case $count >= 300:
  56. $discount = 0.5;
  57. break;
  58. default:
  59. $discount = 1;
  60. }
  61. $total = $count * $price * $discount;
  62. echo "购买数量: $count, 总金额: $total, 折扣率: $discount <hr>";
  63. //switch单值判断
  64. echo "switch 单值判断";
  65. $count = 231;
  66. $discount = 0.4;
  67. $discount = 0.3;
  68. switch ($discount) {
  69. case 0.6:
  70. $total = $count * $price * 0.6;
  71. break;
  72. case 0.5:
  73. $total = $count * $price * 0.5;
  74. break;
  75. case 0.4:
  76. $total = $count * $price * 0.4;
  77. break;
  78. case 0.3:
  79. $total = $count * $price * 0.3;
  80. break;
  81. default:
  82. exit('不支持');
  83. }
  84. echo "购买数量: $count, 总金额: $total, 折扣率: $discount <hr>";

效果图

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议