Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7837521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:40:16+00:00 2026-06-02T14:40:16+00:00

Can someone please explain to me why I get Notice: Undefined variable for variable

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T14:40:19+00:00Added an answer on June 2, 2026 at 2:40 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone please explain to me why in the following code I get an
I don't get it - can someone please explain to me why I should
Could someone please help explain why I can't get this to work? I properly
can someone please explain to me how i can get the Text property of
Can someone please explain this to me? I have the following code: <form action=<?php
Can someone please explain in plain language how this code works to give a
This question was asked before, but it wasn't answered properly. Can someone please explain
Can someone please explain why the following code causes my app to bomb? NSData
Can someone please explain me what's the difference between Swing and AWT? Are there
Can someone please explain why int (0.4 * 10.0) is 4 yet int ((2.4

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.