-1

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;
}
}   
  • Nothing really.. But it's needed to have a foreach with both key and value. The ( doesn't do anything either but you need it. – Andreas May 06 '18 at 06:12
  • 1
    https://stackoverflow.com/questions/1241819/what-does-mean-in-php read this, this question is about that double arrow operator in php. – encryptoferia May 06 '18 at 06:13
  • I'm voting to close this question as off-topic because this question is about how and why a syntax is the way it is and is not suited on SO. – Andreas May 06 '18 at 06:14
  • 3
    Possible duplicate of [What does "=>" mean in PHP?](https://stackoverflow.com/questions/1241819/what-does-mean-in-php) – Charles Shiller May 06 '18 at 06:16
  • it is a seperator in associative arrays .here it means foreach `$val` ( value ) of `$key`. it is like saying look into the value of the key in the `foreach` loop. allows exposing the `$val` – Obmerk Kronen May 06 '18 at 06:18
  • Correction, the second statement doesn't do the opposite. Be aware the opposite would be <= (less or equal). – Juanmi Taboada May 06 '18 at 07:04
  • I am also trying to resolve this problem. `if ($tickets % 10 == 0) { $sum = $tickets *2 - 2; }`........... Now if i insert 10/20/30/40 inside the input field it will calculate the total - 2euro. But at 20 its needs to calculate 20 - 4 and at 30 - 6 at 40 - 8. How do i do that? –  May 06 '18 at 07:18
  • I am asking here because i can't ask questions for 2 day's. I don't understand that, i alway's ask questions after searching the internet for a hour. And i am in the beginning of learning php which i am ambitiouse about so why block my question limit. –  May 06 '18 at 07:37

1 Answers1

2

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
         }
      }
Michel
  • 3,592
  • 4
  • 33
  • 46
  • I am also trying to resolve this problem. `if ($tickets % 10 == 0) { $sum = $tickets *2 - 2; }`........... Now if i insert 10/20/30/40 inside the input field it will calculate the total - 2euro. But at 20 its needs to calculate 20 - 4 and at 30 - 6 at 40 - 8. How do i do that? –  May 06 '18 at 07:22
  • I am asking here because i can't ask questions for 2 day's. I don't understand that, i alway's ask questions after searching the internet for a hour. And i am in the beginning of learning php which i am ambitiouse about so why block my question limit. –  May 06 '18 at 07:38