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

  • Home
  • SEARCH
  • 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 6655761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:33:37+00:00 2026-05-26T01:33:37+00:00

Is there a single function that can be created to tell whether a variable

  • 0

Is there a single function that can be created to tell whether a variable is NULL or undefined in PHP? I will pass the variable to the function (by reference if needed) but I won’t know the name of the variable until runtime.

isset() and is_null() do not distinguish between NULL and undefined.
array_key_exists requires you to know the name of the variable as you’re writing your code.
And I haven’t found a way to determine the name of a variable without defining it.

Edit

I’ve also realized that passing a variable by reference automatically defines it.

Elaboration

Through the collection of these answers and comments I’ve determined that the short answer to my question is "No". Thank you for all the input.

Here are some details on why I needed this:

I’ve created a PHP function called LoadQuery() that pulls a particular SQL query from an array of queries and then prepares it for submission to MySQL. Most-importantly I scan the query for variables (like $UserID) that I then replace with their values from the current scope. In creating this function I needed a way to determine if a variable had been declared, and was NULL, empty, or had a value. This is why I may not know the name of the given variable until runtime.

  • 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-05-26T01:33:38+00:00Added an answer on May 26, 2026 at 1:33 am

    Essentially the answer is no. There is not a single function you can create that will tell whether a runtime variable is null or is undefined. (by ‘runtime variable’ I mean a variable who’s name you don’t yet know at the time of coding. See Elaboration in the question above).

    Relevant Observations:

    • There’s no way to retrieve the name of a variable at runtime without giving it a value and hence declaring it.
    • If you pass a variable by reference, instead of by value, you’re automatically declaring it. So then in the function you can’t go back and determine whether it was declared before you passed it.
    • You can use array_key_exists('variable_name', $GLOBALS) as @zzzzBov stated, to see if a variable has been declared, but only if you know the name of the variable at coding time.

    Possible ‘Dirty’ Solutions

    • As @Phil (and @zzzzBov) explained you could use a messy trick of capturing error messages that would get thrown when you reference an undeclared variable.

    • I also considered a method where you: Make note of all the keys in $GLOBALS, then store a unique value in your target variable (recording it’s original value first for later use). And then search $GLOBALS looking for that unique value to determine the name of the variable AND (by comparing with your earlier look at $GLOBALS) determine if the variable existed before. But this also seems messy and unreliable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a single algorithm that removes elements from a container as happens in
I see in richfaces that there is a single slider, wondering if anyone has
Can someone tell me the correct way of passing multiple vectors to a function
I'm trying to create a set of function templates that can take different types
Is there a single shortcut to start inserting in the new line at end
Is there explicit support for Single Table Inheritance in Django? Last I heard, the
Hi there im using a single TextBox for querying differents types of chossings, for
Is there a difference between single and double quotes in Java?
Is there a way of reading one single character from the user input? For
Is there anyway to combine all resources into a single exe file such as

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.