Blogger Information
Blog 23
fans 0
comment 0
visits 18961
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
if语句 三元 和 函数练习
手机用户1617360551
Original
777 people have browsed it

if语句 三元 和 函数练习

一.if语句

  1. <?php
  2. function demo($price,$num){
  3. //商品总价 = 单价 * 数量
  4. $total = $price * $num;
  5. //当商品总价大于等于3000并且小于5000时,打9折
  6. if($total >= 3000 && $total <5000)
  7. {
  8. $pay = $total * 0.9;
  9. //当商品总价大于等于5000并且小于10000时,打8折
  10. }elseif($total >=5000 && $total <10000)
  11. {
  12. $pay = $total * 0.8;
  13. //当商品总价大于等于10000时,打七折
  14. }elseif($total >= 10000){
  15. $pay = $total * 0.7;
  16. }else{
  17. //默认不打折
  18. $pay = $total;
  19. }
  20. //返回实付金额
  21. return $pay;
  22. }
  23. echo '您的实付金额为:'.demo(888,5);

二.三元

  1. function demo2($price ,$num){
  2. $total = $price * $num;
  3. return $total > 3000 ? '您的消费金额超过3000元,享受会员8折,实付金额:'.$pay = $total * 0.8 .'元!'
  4. : '您的消费总价未满3000元,没有折扣享受,实付:'.$pay = $total.'元!';
  5. }
  6. echo demo2(1999,2);

三.自己练习几个数组函数

  1. //自行练习5个数组函数
  2. //1:
  3. //array_filter,数组过滤函数,返回数组中达到条件的值
  4. $arr = [12,35,77,150,188,203];
  5. $res = array_filter($arr,function($value){return $value >100;});
  6. print_r($res);
  7. //2.
  8. //array_keys(),返回包含数组中所有键名的一个新数组。
  9. $arr1 = ['name'=>'嬴政','position'=>'大秦帝国国君','nickname'=>'屎皇帝'];
  10. $res1 = array_keys($arr1);
  11. print_r($res1);
  12. echo '<br>';
  13. //第二个参数为可选参数,返回指定值的键,即使返回一个值,也是数组,用print_r打印
  14. $res2 = array_keys($arr1,'大秦帝国国君');
  15. print_r($res2);
  16. //3.
  17. //array_map(),将数组中的每个值,用函数处理,返回新数组
  18. function demo3($value){
  19. if($value >100)
  20. {
  21. $value -= 50;
  22. }
  23. return $value;
  24. }
  25. $arr2 = [20,80,120,180,200,230];
  26. //第一个参数是自定义函数名,加上引号
  27. $res = array_map('demo3',$arr2);
  28. print_r($res);
  29. //返回结果,数组中大于100的值,将被减去50
  30. //Array ( [0] => 20 [1] => 80 [2] => 70 [3] => 130 [4] => 150 [5] => 180 )
  31. //4.
  32. //array_rand(),随机返回数组中的一个键名;
  33. $arr3 = ['tang'=>'李世民','song'=>'朱元璋','sui'=>'杨坚','qin'=>'嬴政'];
  34. $key = array_rand($arr3);
  35. echo $key.'<br>';
  36. //设置第二个参数,返回多个键名,
  37. $keys= array_rand($arr3,3);
  38. print_r($keys);
  39. //5.
  40. //array_values(),返回数组中所有的值,
  41. $arr3 = ['tang'=>'李世民','song'=>'朱元璋','sui'=>'杨坚','qin'=>'嬴政'];
  42. $values = array_values($arr3);
  43. print_r($values);

运行结果

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