1

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.

3
  • 1
    try $subtotal = $subtotal + $cartItem['total']; Commented Apr 25, 2012 at 17:46
  • @mgraph assigning values like that will cause a memory leak Commented Apr 25, 2012 at 17:47
  • @mgraph, I get the same error with that, but thanks for helping :) Commented Apr 25, 2012 at 18:02

2 Answers 2

7

Basically the error is saying your using $subtotal variable before its set, or in your case your increasing a value to it before its been set:

$subtotal=0;
foreach( $cart as $cartKey => $cartItem )
{
    $subtotal += $cartItem['total'];
}

Edit: If $subtotal is set perhaps due to some other code setting it or its not set then a check should be made, or you should set it at the start of your script:

$subtotal=(isset($subtotal))?$subtotal:0;
foreach( $cart as $cartKey => $cartItem )
{
    $subtotal += $cartItem['total'];
}

When developing any script its always a good idea to have error_reporting(E_ALL) to give you every error in your code, it helps you learn in the long run. Once you see a few Notice Undefined messages your change the way you code & check for variables. Then when the script is ready for release turn E_ALL to 0

Sign up to request clarification or add additional context in comments.

4 Comments

Im not sure this is a good idea at this point in his code. Seems to me he should be expecting $subtotal to be defined by now. The solution is correct but i think it probably belongs long before he gets to this loop. Of course we cant be sure without seeing all of his code :-)
I see. So in the case of the 1st snippet, it'll just treat it as $zero if I never defined/declared it?
@prodigitalson, this is actually the sample codes from my professor :) I am really just trying to understand it atm.
@Dino55 You should point out to your prof about using undefined variables, you might get extra marks ;p
3

Apprently $subtotal doesnt exist yet in the area where the loop is. Since you are using += you are getting this error beacuse you are essentially saying:

$subtotal = $subtotal + $cartItem['total']

in the cases of $cartKey and $cartItem you 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 => $cartItem you defined those variables.

2 Comments

Thank you very much on explaining $cartkey and $cartItem to me. I think I understand better now. I am used to declaring variables before using it so I was confused on if I missed something. Thanks! :) Another question, how can I access these 2 variables on another web page? will they be saved in the $cart array?
No they are pretty much temporary.... Well when the loop is finished whatever the last values for those variables are will persist in the rest of the code with the same scope, but you shouldnt trust using them at that point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.