PHP变量和数组遍历以及初始函数
发布于:2022-03-10 09:14:22
次阅读
变量与数组
变量的 8 种类型
// 变量的8种类型// 1. 整型int$int = 1;echo $int ."<br/>";// 2.字符串string$string = "我是字符串";echo $string ."<br/>";// 3.布尔型boole: 只用两个变量 true和false用于条件判定$isTrue = false;var_dump($isTrue);echo "<br/>";// 4.浮点型font (小数点)$font = 0.2543;var_dump($font);// 5.对象object$obj = new stdClass;var_dump($obj); // 结果object(stdClass)[1]// 6.资源 resource (保存到外部资源的一个引用)$handle = fopen('log.log', 'w');//在本目录新建一个log.log的文件// var_dump($handle);// 7.null 只是变量没有值 不代表变量内容为0 ,也不代表" ";$null = null;var_dump($null); //值为 null// 8 数组$shopping = [ ['name' => '苹果手机', 'priced' => 4080 , 'quanity' => 2], ['name' => '笔记本', 'priced' => 6708 , 'quanity' => 2], ['name' => '小米电视', 'priced' => 8080 , 'quanity' => 1],];
数组的遍历
<body> <div> <ul> <?php foreach($shopping as $k => $v){ ?> <li>名称:<?= $v['name']?> | 价格:<?= $v['priced']?>| <?= $v['quanity']?> </li> <?php } ?> </ul> </div> <div> <ul> <?php for($i = 0; $i < count($shopping); $i++){ ?> <li> 名称: <?= $shopping[$i]['name']?>| 价格: <?= $shopping[$i]['priced']?>| 数量: <?= $shopping[$i]['quanity']?>| </li> <?php } ?> </ul> </div></body>
函数计算购物车
$price = 189;//商品价格$quantity = 12;//商品数量function total ($price, $quantity){ $total = $price * $quantity; return "您的购物车总共价格{$total}元";}echo total($price, $quantity);