Blogger Information
Blog 13
fans 0
comment 0
visits 9135
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP变量和数组遍历以及初始函数
ianren
Original
446 people have browsed it

变量与数组

变量的 8 种类型

  1. // 变量的8种类型
  2. // 1. 整型int
  3. $int = 1;
  4. echo $int ."<br/>";
  5. // 2.字符串string
  6. $string = "我是字符串";
  7. echo $string ."<br/>";
  8. // 3.布尔型boole: 只用两个变量 true和false用于条件判定
  9. $isTrue = false;
  10. var_dump($isTrue);
  11. echo "<br/>";
  12. // 4.浮点型font (小数点)
  13. $font = 0.2543;
  14. var_dump($font);
  15. // 5.对象object
  16. $obj = new stdClass;
  17. var_dump($obj); // 结果object(stdClass)[1]
  18. // 6.资源 resource (保存到外部资源的一个引用)
  19. $handle = fopen('log.log', 'w');//在本目录新建一个log.log的文件
  20. // var_dump($handle);
  21. // 7.null 只是变量没有值 不代表变量内容为0 ,也不代表" ";
  22. $null = null;
  23. var_dump($null); //值为 null
  24. // 8 数组
  25. $shopping = [
  26. ['name' => '苹果手机', 'priced' => 4080 , 'quanity' => 2],
  27. ['name' => '笔记本', 'priced' => 6708 , 'quanity' => 2],
  28. ['name' => '小米电视', 'priced' => 8080 , 'quanity' => 1],
  29. ];

数组的遍历

  1. <body>
  2. <div>
  3. <ul>
  4. <?php foreach($shopping as $k => $v){ ?>
  5. <li>名称:<?= $v['name']?> | 价格:<?= $v['priced']?>| <?= $v['quanity']?> </li>
  6. <?php } ?>
  7. </ul>
  8. </div>
  9. <div>
  10. <ul>
  11. <?php for($i = 0; $i < count($shopping); $i++){ ?>
  12. <li>
  13. 名称: <?= $shopping[$i]['name']?>|
  14. 价格: <?= $shopping[$i]['priced']?>|
  15. 数量: <?= $shopping[$i]['quanity']?>|
  16. </li>
  17. <?php } ?>
  18. </ul>
  19. </div>
  20. </body>

函数计算购物车

  1. $price = 189;//商品价格
  2. $quantity = 12;//商品数量
  3. function total ($price, $quantity){
  4. $total = $price * $quantity;
  5. return "您的购物车总共价格{$total}元";
  6. }
  7. echo total($price, $quantity);
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