Can someone please explain to me why I get “Notice: Undefined variable” for variable $subtotal in the 2nd code snippet but NOT in the 1st code snippet ? What’s the difference between them? Are they consider local variables?
Also, exactly what is the variable type (Global, Superglobal.. etc) of $cartKey and $cartItem in the foreach loop? How come I didn’t need to define/declare them?
switch( $_SESSION['shippingMethod'] )
{
case "Air":
$shipping = $subtotal * 0.1;
break;
}
and
foreach( $cart as $cartKey => $cartItem )
{
$subtotal += $cartItem['total'];
}
Thank you very much in helping.
Apprently
$subtotaldoesnt exist yet in the area where the loop is. Since you are using+=you are getting this error beacuse you are essentially saying:in the cases of
$cartKeyand$cartItemyou did define them… they are part of the loop structure you create them from the current key and value of the array item – “copying” them into the current scope from the array. Eg. by doing$cartKey => $cartItemyou defined those variables.