-4

hi there i got a php code from some tutorial but i can't understand the use of [] in front of the variables, can someone explain this code please.

$text= "KKE68TSA76 Confirmed on 30/03/17 at 2:12PM Ksh100.00 received from 254786740098";
        }
        $mpesa =explode(" ", $text);
        $receipt=$mpesa[0]; // Code    
        $pesa=$mpesa[5]; //
        $p = explode("h", $pesa);
        $decimal=$p[1]; // Amount with decimal
        $dc = explode(".", $decimal);
        $koma=$dc[0]; // Payment
        $ondoa = explode(",", $koma);
        $kwanza=$ondoa[0]; // Payment
        $pili=$ondoa[1]; // Payment
        $payment=$kwanza.$pili;
        $phone=$mpesa[8]; // Phone
uji moto
  • 1
  • 4

2 Answers2

1

The [ ] is an array position. Exploding $mpesa turns that string of text into an array split by every space. $mpesa[0] is array position one, containing KKE68TSA76, $mpesa[1] contains Confirmed.. etc

clearshot66
  • 2,205
  • 1
  • 6
  • 16
0

The [ ] us an array positioner, so it indicates the position of an element in the list/array.

But what are arrays?

An array is a special variable, which can hold more than one value at a time. - W3Schools

$array = array(
  "Item 1", // Position 0
  "Item 2", // Position 1
  "Item 3" // Position 2
);

echo $array[0]; // THIS WILL OUTPUT: "Item 1".
echo $array[1]; // THIS WILL OUTPUT: "Item 2".
echo $array[2]; // THIS WILL OUTPUT: "Item 3".

I hope this can be useful.

Federico
  • 340
  • 3
  • 12