Can someone explain me a php code

So i was searching it online but i couldn't find a basic explanation. I am new with php. So i am going to say what i understand out of this code.

foreach is that it's using multiple $name and the AS makes the first variable the same as the second $key but then comes => which i don't understand.

if $min higher than $val , $min = $val and the one under is the opposite.

What is => exactly doing?

foreach($arr as $key => $val){
if($min > $val){
$min = $val;
}
 if($max < $val){
$max = $val;
}
}   

Suppose you have an array:

$array = [
   'monkey' => 1,
   'dog'    => 2,
   'bird' => 3
   ];

The foreach loops trough all the elements of the array:

foreach ( $array as $key => $value){ ... }
             ^        ^        ^
             |        |        |
         the array   the key:  the value:
                     monkey       1
                     dog          2
                     bird         3

Inside the foreach you can then manipulate the array. For example:

foreach ( $array as $key => $value){
    if( $value > 1 )                        //true for dog and bird
        {
        $array [ $key ] = $value + 10;      //dog now is 12, bird becomes 13
        }
    }

If you only need the values, you can leave the key => part out:

foreach ( $array as $value){
      if( $value > 1 )
         { 
         echo $value;                       // echo's 2 and 3
         }
      }
链接地址: http://www.djcxy.com/p/58184.html

上一篇: 想让我的用户能够删除一个mysql行

下一篇: 有人可以解释我一个PHP代码