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 157571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:31:40+00:00 2026-05-11T10:31:40+00:00

What is the best way to define that a value does not exist in

  • 0

What is the best way to define that a value does not exist in PHP, or is not sufficent for the applications needs.

$var = NULL, $var = array(), $var = FALSE?

And what is the best way to test?

isset($var), empty($var), if($var != NULL), if($var)?

Initializing variables as what they will be, e.g. NULL if a string, array() if they will be arrays, has some benefits in that they will function in the setting they are ment to without any unexpected results.

e.g. foreach($emptyArray) won’t complain it just wont output anything, whereas foreach($false) will complain about the wrong variable type.

But it seams like an unnecessary hassle to have so many different ways of doing basically the same thing. eg. if(empty($var)) or if ($var == NULL)


Duplicate: Best way to test for a variable’s existence in PHP; isset() is clearly broken

  • 1 1 Answer
  • 5 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. 2026-05-11T10:31:40+00:00Added an answer on May 11, 2026 at 10:31 am

    Each function you named is for different purposes, and they should be used accordingly:

    • empty: tells if an existing variable is with a value that could be considered empty (0 for numbers, empty array for arrays, equal to NULL, etc.).
    • isset($var): tells if the script encountered a line before where the variable was the left side of an assignment (i.e. $var = 3;) or any other obscure methods such as extract, list or eval. This is the way to find if a variable has been set.
    • $var == NULL: This is tricky, since 0 == NULL. If you really want to tell if a variable is NULL, you should use triple =: $var === NULL.
    • if($var): same as $var == NULL.

    As useful link is https://www.php.net/manual/en/types.comparisons.php.

    The way to tell if the variable is good for a piece of script you’re coding will entirely depend on your code, so there’s no single way of checking it.

    One last piece of advice: if you expect a variable to be an array, don’t wait for it to be set somewhere. Instead, initialize it beforehand, then let your code run and maybe it will get overwritten with a new array:

    // Initialize the variable, so we always get an array in this variable without worrying about other code. $var = array();  if(some_weird_condition){   $var = array(1, 2, 3); }  // Will work every time. foreach($var as $key => $value){ } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does someone knows C# best practice about the way to define attribute visibility (private
Can you suggest a best way to define money type in F#?
Given following data, what is the best way to organize an array of elements
What is the best way to define a numerical constant in Mathematica? For example,
the best way to explain is with example so: this is the model public
The best way I can think of to ask this is by example... In
The best way of describing this is I have a table of people with
The best way to store images into MySQL is by storing the image location
What is currently the best way to authorise users of a Rails app using
What is the best way to show a div when clicked on a button

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.