Blogger Information
Blog 3
fans 0
comment 0
visits 1291
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
[技术博客] 实例演示分支与循环, Switch 的使用方法
P粉912642467
Original
480 people have browsed it

assignment: 0809

分流

  1. $product = ['catagroy' => 'shoe', 'id' => '0001', 'price' => 160];
  2. printf("Catagroy:%s" . ", ID: %s" . ", Price: %s", $product['catagroy'], $product['id'], $product['price']);
  3. function showProduct(string $cata, string $id, int $price): string
  4. {
  5. return "The new product is " . $cata . ", it's ID is " . $id . ", and the price is " . $price;
  6. }
  7. printf(showProduct($product['catagroy'], $product['id'], $product['price']));
  8. echo call_user_func('showProduct', $product['catagroy'], $product['id'], $product['price']);
  9. echo "<hr>";
  10. echo call_user_func_array('showProduct', $product);
  11. echo "<hr>";

使用对象函数

  1. class newProduct
  2. {
  3. public function newShoes(string $brand, string $color)
  4. {
  5. return "The brand new shoes is form " . $brand . ", and the color is " . $color . ".";
  6. }
  7. }
  8. $obj = new newProduct;
  9. $pg6 = ['nike', 'green'];
  10. // echo $obj->newShoes($pg6[0],$pg6[1]);
  11. echo call_user_func_array([$obj, 'newShoes'], $pg6);

流程控制: 分支

  1. $price = 300;
  2. switch (true) {
  3. case $price < 200:
  4. echo "The price is lower than 200";
  5. break;
  6. case $price = 200:
  7. echo "The price is equal to 200";
  8. break;
  9. case $price > 200:
  10. echo "The price is over 200";
  11. break;
  12. default:
  13. echo "The price is invaild";
  14. break;
  15. }
  16. echo "<hr>";

循环

  1. $shoeInStock = ['Kyrie7','KD15','PG6'];
  2. class shoeStore{
  3. public function displayShoes(){
  4. for($i=0;$i<sizeof($GLOBALS["shoeInStock"]);$i++){
  5. echo ($GLOBALS["shoeInStock"])[$i]."<br>";
  6. }
  7. }
  8. }
  9. $obj2 = new shoeStore;
  10. call_user_func_array([$obj2,'displayShoes'],$shoeInStock);

练习foreach

  1. $users = [
  2. 0=>['id'=>5,'name'=>'猪老师', 'gender'=>0, 'score'=>88],
  3. 1=>['id'=>6,'name'=>'张老师', 'gender'=>1,'score'=>68],
  4. 2=>['id'=>7,'name'=>'狗老师','gender'=>1, 'score'=>98],
  5. ];
  6. foreach ($users as $value){
  7. echo '学号:'.$value['id']. ", 姓名:".$value['name'].", 分数:".$value['score'].", 性别:";
  8. if($value['gender']==0){echo "male";}else{echo "female";}
  9. echo "<br>";
  10. }
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