tags:

views:

85

answers:

5

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..??

A: 

You're referencing an undefined index while passing it initially. You could do this:

$key = 'id';

function foo($id ) {
    if ( isset( $_GET[$id] ) ) { 
    }
}

Alternatively you could just do isset before the function call.

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

Another option, use a temporary variable:

$foo = isset( $_GET['id'] ) ? $_GET['id'] : null;
if ( $foo ) { functionCall($foo); }

You can also tone down the error reporting levels if it doesn't concern you that much.

meder
A: 
mmattax
He knows that. He wanted to _avoid_ that, too!
msakr
+2  A: 

I always include a convenience function that a few other languages provide: get.

function get($needle, $haystack, $return=null) {
    return array_key_exists($needle, $haystack) ? $haystack[$needle] : $return;
}

Now you can call:

if(isNum(get('id', $_GET))) {
    // do something here
}
thetaiko
Thanks. Simple and effective.
pnichols
A: 

Here is an elegant way of doing it using a custom class:

First, define a SmartArray class:

class SmartArray extends ArrayObject {
    public static function convert(&$array) {
        $array = new self($array);
    }
    public function offsetGet($index) {
        return $this->offsetExists($index)? parent::offsetGet($index):NULL;
    }
}

Then simply call:

SmartArray::convert($_GET);

Now your if(isNum($_GET['id'])) { ... } won't complain anymore if $_GET doesn't have the key 'id' :)

Lukman
+1  A: 

You can change isNum to receive the parameter by reference

<?php

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

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

However, $_GET['id'] will be implicitly created on call and initialized to NULL.

If you're not OK with that or with the hacky nature of this solution, go with some other answer.

Artefacto
Thanks for the improved code. (sanitize after isset, return ctype_digit()...)
pnichols