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

1
  • 1
    besides your actual problem: instead of "if (<condition>) { return true; } else { return false; }" you can just write "return <condition>;" :-) Commented Jun 14, 2010 at 15:18

5 Answers 5

4

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Simple and effective.
4

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.

1 Comment

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

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.

Comments

0

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' :)

Comments

-2

This is what you want, check to make sure the id is set BEFORE the call to isNum:


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

1 Comment

He knows that. He wanted to avoid that, too!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.