PHP - Use isset inside function not working..?

Posted by pnichols on Stack Overflow See other posts from Stack Overflow or by pnichols
Published on 2010-06-14T15:06:30Z Indexed on 2010/06/14 15:12 UTC
Read the original article Hit count: 137

Filed under:
|

I have a PHP script that when loaded, check first if it was loaded via a POST, if not if GET['id'] is a number.

Now I know I could do this like this:

if(isset($_GET['id']) AND isNum($_GET['id'])) { ... }

function isNum($data) {  
    $data = sanitize($data);
    if ( ctype_digit($data) ) {
        return true;
    } else {
        return false;
    }
}

But I would like to do it this way:

if(isNum($_GET['id'])) { ... }

function isNum($data) {  
    if ( isset($data) ) {
        $data = sanitize($data);
        if ( ctype_digit($data) ) {
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}

When I try it this way, if $_GET['id'] isn't set, I get a warning of undefined index: id... It's like as soon as I put my $_GET['id'] within my function call, it sends a warning... Even though my function will check if that var is set or not...

Is there another way to do what I want to do, or am I forced to always check isset then add my other requirements..??

© Stack Overflow or respective owner

Related posts about php

Related posts about functions