Hello!

I'm quite new in PHP, so I have the following question:

When I read a post/get variable with $HTTP_GET_VARS['varname'] or $HTTP_POST_VARS['varname'] and if that variable is not passed to php script, I get an annoying warning on my HTML page - that variable was not found in that collection.

The example:

if ($HTTP_GET_VARS['var1'] = 3) {
// Do something

}

  1. script.php?var1=3
    That works fine, because it finds var1 as passed variable.
  2. script.php
    Here I don't pass variables, however in script there is still that if statement and it CAUSES WARNING, displayed on my HTML site.
    --------------------

How can I get rid of that?

Regards
Zvonko

    Don't you really mean : if ($HTTP_GET_VARS['var1'] == 3) instead of if ($HTTP_GET_VARS['var1'] = 3) ?

    And, if you don't pass any variables using the GET method, then the $HTTP_GET_VARS (and $HTTP_POST_VARS, if you use POST) won't be available... so why not check for the array before you try reading from it?

      • [deleted]

      You may also get that warning when HTTP_GET_VARS does exist, but some element of it doesn't.

      Read about the isset() function and use it!

        Thanks, guys.

        If you had any inconveniance, I appologize, but as I told in my first post I'm a beginner in php (born a few days ago ;o).

        Thanks again

          • [deleted]

          No problem, that's what we're here for! 🙂

            I prefer the empty() function.

              • [deleted]

              Not good. empty() evaluates to FALSE if the variable contains a zero.

              From the manual:
              --quote--
              $var = 0;

              if (empty($var)) { // evaluates true
              echo '$var is either 0 or not set at all';
              }

              if (!isset($var)) { // evaluates false
              echo '$var is not set at all';
              }
              --end quote--

              So you cannot use 'empty()' for example to check if someone has entered an amount in a shoppingcart. He may have entered zero, but instead your script thinks he did not enter anything at all.

                I agree with the "0" problems but then again you run into another problem altogether when using "isset" with posted information.

                From the php.manual:

                $HTTP_POST_VARS is a pre-defined variable in PHP, do not confuse between this var being empty and being undefined/unset, if you want to check whether the vars are coming from a POST method, use empty() instead.]

                isset($HTTP_POST_VARS) returns TRUE, even if there hasn't been a form posted, and phpinfo() confirms that the variable doesn't exist.

                  • [deleted]

                  How is that a problem?

                  When would it ever be usefull to test for the HTTP_POST_VARS's existence?

                  You can use $REQUEST_METHOD to find out wether data was posted or 'getted', and every post from a form contains at least one var: the submit button.

                    Write a Reply...