Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8019329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:21:30+00:00 2026-06-04T21:21:30+00:00

In the past when I needed to check if a variable was set and

  • 0

In the past when I needed to check if a variable was set and also a number, I would do:

if( isset($_GET['var']) && is_numeric($_GET['var']) )

But I think that’s kind of ugly, especially when I need to check a bunch of variables in the same if statement, so I made a function:

function setAndNum($var)
{
    if(isset($var) && is_numeric($var))
        return 1;
    else
        return 0;
}

The problem is that when I pass an undefined variable to the function, like this (supposing the variable in the GET array is undefined):

if( setAndNum($_GET['var']) )

I get the php error:

Notice: Undefined index: …

So the whole purpose of the function is basically defeated (or half the purpose, at least 😉 ).

One thing that confuses me is how the isset() function works, and why I can pass an undefined variable to it but not to my own function?

Is it possible to make my setAndNum() function work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T21:21:32+00:00Added an answer on June 4, 2026 at 9:21 pm

    Your problem is with $_GET being an array. When you pass $_GET['var'] to your function, this array value is already looked up and used as an argument to the function. Therefore you cannot effectively check the presence of 'var' in $_GET from within this function. You could rewrite it a bit to make it work for array values, something like this:

    function setAndNum($key, $array)
    {
        if(array_key_exists($key, $array) && is_numeric($array[$key]))
            return 1;
        else
            return 0;
    }
    

    Then call it like this:

    if( setAndNum('var', $_GET) )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to generate a data set, and I think recursion is required, but
So my code in the past needed a variable to run through 2 functions
In my past applications I have been #importing into my *.h files where needed.
I was using the $.click() method for triggering some events. But then I needed
In the past, when I've needed array-like indexical lookups in a tight loop, I
I've been using Liferay a lot for past 2 years, but I have never
i've tried everything for the past 2 hours to get this working but my
I think I've been doing this wrong for the past year and a half
In the past, whenever I needed to get the value of an html element,
I know that similar questions have been asked in the past but mine has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.