Knowledge summary of PHP process control (with examples)

不言
Release: 2023-04-05 13:08:02
forward
2116 people have browsed it

The content of this article is a summary of knowledge about PHP process control (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

  1. There are three ways to traverse arrays in PHP: for loop, foreach loop, while, list(), each() combined loop

  2. Arrays in PHP are divided into: index array [converted to json is an array] and associative array [converted to json is an object]

  3. For loop can only traverse index arrays, foreach can traverse index arrays and associative arrays, while, list(), and each() combined loops can also traverse index arrays and associative arrays

  4. While, list(), and each() combination will not reset() the array pointer

  5. foreach traversal will reset() the array )Operation

  6. php branch: if...elseif (a basic principle: put the most likely conditions first for processing)

  7. php branch: switch...case... (the data type of the control expression behind switch can only be: integer, floating point type or string), use the continue function in switch and Same as break, to jump out of the switch outer loop, use continue num, break num, break num is to end the entire loop body of the numth outer layer, continue num is to end the single loop of the outer numth layer

  8. switch...case...in PHP will generate a jump table (underlying usage principle), jump directly to the corresponding case, unlike if elseif, which goes through layers of judgments

  9. Tips for improving the efficiency of branch judgment: If the judgment is more complex and only integers, floating point types or strings are judged, you can use switch processing, which will improve efficiency

Proof example:

<?php

$arr = ["apple", "pear", "banana", "orange", "lemon", "strawberry"]; ;

end($arr); //数组指针指向最后一个值

var_dump("打印当前数组指针对应的值:".current($arr)); //打印当前数组指针对应的数组

foreach ($arr as $key => $val){
    var_dump("打印foreach循环当前数组指针对应的值:".$val);
    if($key == 3){
        break;
    }
}
var_dump("打印当前数组指针对应的值:".current($arr)); //打印当前数组指针对应的数组

while($element = each($arr)) {
    var_dump($element);
}


//输出结果:
string &#39;打印当前数组指针对应的值:strawberry&#39; (length=49)
string &#39;打印foreach循环当前数组指针对应的值:apple&#39; (length=57)
string &#39;打印foreach循环当前数组指针对应的值:pear&#39; (length=56)
string &#39;打印foreach循环当前数组指针对应的值:banana&#39; (length=58)
string &#39;打印foreach循环当前数组指针对应的值:orange&#39; (length=58)
string &#39;打印当前数组指针对应的值:lemon&#39; (length=44)
array (size=4)
  1 => string &#39;lemon&#39; (length=5)
  &#39;value&#39; => string &#39;lemon&#39; (length=5)
  0 => int 4
  &#39;key&#39; => int 4
array (size=4)
  1 => string &#39;strawberry&#39; (length=10)
  &#39;value&#39; => string &#39;strawberry&#39; (length=10)
  0 => int 5
  &#39;key&#39; => int 5
Copy after login

The above is the detailed content of Knowledge summary of PHP process control (with examples). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template