Re: Re: Avoiding NOTICEs on Array References

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



trlists@xxxxxxxxxx wrote:
On 26 Jan 2005 Jason Barnett wrote:


if (isset($_POST['checkboxfieldname'])) {
  /** do stuff */
}


Sorry, I should have mentioned that I knew about using isset -- it works OK for the checkbox example, though I'm not clear if this behavior is specified and therefore will not change -- i.e. can one rely on the fact that the checkbox field name is missing entirely from _POST if the box is not checked? Or are there cases where it could be present but with an empty or NULL value?

I can't see how it can be set to null unless you do it yourself - actually its all strings isn't it? but it could be an empty string if the checkbox value was set to that.

If one must check the value and not just the existence of the checkbox entry, or for other uses, e.g. where a flag may or may not be present, one is saddled with clumsy constructs like:


	if (($isset($array['index']) && ($array['index'] == 1)) ...


okay, I get where your coming from - indeed nasty business them checkboxes.



function getGP($v = '', $r = null, $t = null)
{
    if (!empty($v)) {
        if (isset($_GET[$v]))  { $r = (!is_null($t)) ? $t: $_GET[$v]; }
        if (isset($_POST[$v])) { $r = (!is_null($t)) ? $t: $_POST[$v];}
    }

    return $r;
}


$state = getGP('mychkbox', false, true); -- $state will be a boolean.

$state = getGP('mychkbox', false, true);
--
$state will be an 'id' or false.


if you were expecting an array of checkbox values e.g.

<input type="checkbox" name="mycheckboxes[]" value="1"  />
<input type="checkbox" name="mycheckboxes[]" value="2"  />
<input type="checkbox" name="mycheckboxes[]" value="3"  />

then you need a little fancier function to cope.

I would prefer that the second expression return FALSE, or that $array['index'] where the index is not present simply return NULL -- or probably better, an option to avoid E_NOTICE level errors for such cases.

its E_NOTICE v. the Empty()-isset() Brothers at the OK Corral I'm afraid. basically it comes down to wrapping the checks is simple(looking?) functions.

or turn off notices. may turn them off only in the place where
you grab/check incoming vars? but that seems lame.

anyway I just ran this on ...

PHP 5.0.2 (cli) (built: Oct 21 2004 13:52:27)

$> php -r 'echo error_reporting(E_STRICT | E_ALL);echo error_reporting();
if (!@$GLOBALS["chumpy_joodab_revmoor"]) { echo "yeah"; }
'
20394095yeah

thats interesting. :-)


-- Tom


-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux