How to get rid of warnings?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lenard Redwood

    #1

    How to get rid of warnings?

    I have the maximum number of warnings during PHP development. How can I
    get rid of these warnings without turning them off in PHP?

    Here's the code I have:

    <form method="POST" action="eat.php ">
    <select name="lunch[ ]" multiple>
    <option value="pork">BB Q Pork Bun</option>
    <option value="chicken" >Chicken Bun</option>
    <option value="lotus">L otus Seed Bun</option>
    <option value="bean">Be an Paste Bun</option>
    <option value="nest">Bi rd-Nest Bun</option>
    </select>
    <input type="submit" name="submit">
    </form>

    Selected buns: <br/>
    <?php
    foreach ($_POST['lunch'] as $choice) {
    print "You want a $choice bun. <br/>";
    }
    ?>


    And I get: Warning: Invalid argument supplied for foreach() in
    C:\lighttpd\htd ocs\index.php on line 17

    Is there a way to give the 'lunch' variable a default value if
    undefined?
    Thank you.

  • Jerry Stuckle

    #2
    Re: How to get rid of warnings?

    Lenard Redwood wrote:
    I have the maximum number of warnings during PHP development. How can I
    get rid of these warnings without turning them off in PHP?
    >
    Here's the code I have:
    >
    <form method="POST" action="eat.php ">
    <select name="lunch[ ]" multiple>
    <option value="pork">BB Q Pork Bun</option>
    <option value="chicken" >Chicken Bun</option>
    <option value="lotus">L otus Seed Bun</option>
    <option value="bean">Be an Paste Bun</option>
    <option value="nest">Bi rd-Nest Bun</option>
    </select>
    <input type="submit" name="submit">
    </form>
    >
    Selected buns: <br/>
    <?php
    foreach ($_POST['lunch'] as $choice) {
    print "You want a $choice bun. <br/>";
    }
    ?>
    >
    >
    And I get: Warning: Invalid argument supplied for foreach() in
    C:\lighttpd\htd ocs\index.php on line 17
    >
    Is there a way to give the 'lunch' variable a default value if
    undefined?
    Thank you.
    >
    Make sure $_POST['lunch'] is set and is an array. Check out isset() and
    is_array().

    And ALWAYS validate information coming from the user. NEVER assume
    you're getting something.

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • Gucci

      #3
      Re: How to get rid of warnings?

      try
      <?php
      foreach ($lunch as $choice) {
      print "You want a $choice bun. <br/>";
      }
      ?>

      Comment

      • Juliette

        #4
        Re: How to get rid of warnings?

        Gucci wrote:
        try
        <?php
        foreach ($lunch as $choice) {
        print "You want a $choice bun. <br/>";
        }
        ?>
        >
        RED ALERT ! never work with register_global s on

        Comment

        • Manish

          #5
          Re: How to get rid of warnings?

          <?php

          if(is_array($_P OST['lunch']) && count($_POST['lunch'])) {
          foreach ($_POST['lunch'] as $choice) {
          print "You want a $choice bun. <br/>";
          }
          }

          ?>

          Comment

          • Lenard Redwood

            #6
            Re: How to get rid of warnings?


            Manish wrote:
            <?php
            >
            if(is_array($_P OST['lunch']) && count($_POST['lunch'])) {
            foreach ($_POST['lunch'] as $choice) {
            print "You want a $choice bun. <br/>";
            }
            }
            >
            ?>
            Thank you. Now I get this error:

            Notice: Undefined index: lunch in C:\lighttpd\htd ocs\index.php on line
            17

            Comment

            • Jerry Stuckle

              #7
              Re: How to get rid of warnings?

              Lenard Redwood wrote:
              Manish wrote:
              >
              >><?php
              >>
              >>if(is_array($ _POST['lunch']) && count($_POST['lunch'])) {
              > foreach ($_POST['lunch'] as $choice) {
              > print "You want a $choice bun. <br/>";
              > }
              >>}
              >>
              >>?>
              >
              >
              Thank you. Now I get this error:
              >
              Notice: Undefined index: lunch in C:\lighttpd\htd ocs\index.php on line
              17
              >
              See my earlier suggestion. Before checking if it's an array, you should
              see if it's even set:

              if(isset($_POST['lunch']) && is_array($_POST['lunch']) &&
              count($_POST['lunch'])) {
              foreach ($_POST['lunch'] as $choice) {
              print "You want a $choice bun. <br>";
              }
              }

              BTW - <br/is valid for xml but not html.

              --
              =============== ===
              Remove the "x" from my email address
              Jerry Stuckle
              JDS Computer Training Corp.
              jstucklex@attgl obal.net
              =============== ===

              Comment

              • Charles

                #8
                Re: How to get rid of warnings?


                Jerry Stuckle wrote:
                See my earlier suggestion. Before checking if it's an array, you should
                see if it's even set:
                >
                if(isset($_POST['lunch']) && is_array($_POST['lunch']) &&
                count($_POST['lunch'])) {
                foreach ($_POST['lunch'] as $choice) {
                print "You want a $choice bun. <br>";
                }
                }

                Yes! Thank you Jerry! Working great now :)

                Comment

                • Chuck Anderson

                  #9
                  Re: How to get rid of warnings?

                  Jerry Stuckle wrote:
                  Lenard Redwood wrote:
                  >
                  >Manish wrote:
                  >>
                  >>
                  >><?php
                  >>>
                  >>if(is_array($ _POST['lunch']) && count($_POST['lunch'])) {
                  >> foreach ($_POST['lunch'] as $choice) {
                  >> print "You want a $choice bun. <br/>";
                  >> }
                  >>}
                  >>>
                  >>?>
                  >>>
                  >Thank you. Now I get this error:
                  >>
                  >Notice: Undefined index: lunch in C:\lighttpd\htd ocs\index.php on line
                  >17
                  >>
                  >>
                  >
                  See my earlier suggestion. Before checking if it's an array, you should
                  see if it's even set:
                  >
                  if(isset($_POST['lunch']) && is_array($_POST['lunch']) &&
                  count($_POST['lunch'])) {
                  foreach ($_POST['lunch'] as $choice) {
                  print "You want a $choice bun. <br>";
                  }
                  }
                  >
                  BTW - <br/is valid for xml but not html.
                  >
                  >
                  "&& count($_POST['lunch'])" seems superfluous here. If it's an array
                  (empty or not), then foreach will not issue a warning.

                  --
                  *************** **************
                  Chuck Anderson • Boulder, CO

                  *************** **************

                  Comment

                  • william.clarke

                    #10
                    Re: How to get rid of warnings?


                    Jerry Stuckle wrote:
                    See my earlier suggestion. Before checking if it's an array, you should
                    see if it's even set:
                    >
                    if(isset($_POST['lunch']) && is_array($_POST['lunch']) &&
                    count($_POST['lunch'])) {
                    foreach ($_POST['lunch'] as $choice) {
                    print "You want a $choice bun. <br>";
                    }
                    }
                    >
                    BTW - <br/is valid for xml but not html.
                    >
                    --
                    =============== ===
                    Remove the "x" from my email address
                    Jerry Stuckle
                    JDS Computer Training Corp.
                    jstucklex@attgl obal.net
                    =============== ===
                    <br /for XHTML
                    <brfor HTML

                    Comment

                    Working...