Blogger Information
Blog 4
fans 0
comment 0
visits 2427
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP的8种数据变量、for&foreach循环语句及函数的使用
远程
Original
875 people have browsed it

一、PHP共有8中类型变量

(1)标量数据类型:

1.boolean布尔

只有两个值 true 和 flase

  1. $isDiscount=true;
  2. //$isDiscount=flase;
  3. var_dump($isDiscount);

2.string字符串

可以储存一切字符的集合。

  1. $productName1="计算器";
  2. $productName2="computer";
  3. var_dump($productName1);

3.integer整数

包括正整数和负整数,无小数位

  1. $number=5;
  2. var_dump($number);

4.double浮点数

用于储存数字,有小数位

  1. $price=64.8;
  2. var_dump($price);

(2)符合数据类型:
5.array数组

5.1 一维数组,一切类型相同的变量组合

  1. $goods=array("name"=>"phone","price"=>"2000","number"=>"1");
  2. var_dump($goods);

5.2 二维数组及多维数组

  1. $shoppingCart=array(
  2. array("name"=>"phone","price"=>"2000","number"=>"1"),
  3. array("name"=>"notebook","price"=>"4000","number"=>"2"),
  4. array("name"=>"USBdisk","price"=>"120","number"=>"3")
  5. );
  6. var_dump($shoppingCart);

6.object对象类型

对象是对象的事例,使用”new 类()”构造函数复制类,类的克隆品称为类的事例.

  1. $shoppingCart=new ShoppingCart(); //ShoppingCart是已经声明的类型

(3)特殊数据类型:
7.resource资源,资源是php内的几个函数中所需要的特殊数据类型,由编写人员分配

8.null空值,没有为给变该量设置任何值,不区分大小写。

  1. $res=null;

二、foreach与for循环

打印出购物车内商品的价格

  1. //方法一:
  2. for ($i = 0; $i <= count($shoppingCart)-1; $i++) {
  3. echo $shoppingCart[$i]['name']."的价格是".$shoppingCart[$i]['price'].'<br>';
  4. }
  5. //方法二:
  6. foreach($shoppingCart as $goods) {
  7. echo $goods['name']."的价格是".$goods["price"].'</br>';
  8. };

三、计算购物车内商品的总价

  1. foreach($shoppingCart as $goods) {
  2. echo $goods['name']."的价格是".$goods["price"].'</br>';
  3. };
  4. function payment_amount(array $shoppingCart)
  5. {
  6. $amount=0;
  7. foreach($shoppingCart as $goods) {
  8. $amount+=$goods['price']*$goods['number'];
  9. }
  10. return $amount;
  11. };
  12. echo payment_amount($shoppingCart);
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