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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:46:45+00:00 2026-06-06T09:46:45+00:00

Is there any more efficient,cleaner and shorter form for following: if($x==null or $y==null or

  • 0

Is there any more efficient,cleaner and shorter form for following:

if($x==null or $y==null or $z==null or $w==null) return true;

In general how can I also get name of null variables?

function any_null(array $a){
    foreach($a as $v) if ($v==null) return //variable name;
    return false;
}
var_dump(any_null(array($x,$y,$z,$w)));
  • 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-06T09:46:47+00:00Added an answer on June 6, 2026 at 9:46 am

    isset can take multiple arguments.

    If multiple parameters are supplied then isset() will return TRUE only
    if all of the parameters are set.

    Since this is the opposite of what you want a simple negation is needed:

    !isset($x, $y, $z, $w)
    

    An additional bonus is that isset is not a regular function and will not raise any notices on undefined variables.


    It’s harder to get the name from the variables. You will need to check every one of them in some way. I think the best and most scalable solution is to use an associative array with the name of the variable as key:

    function any_null(array $a){
        foreach($a as $k => $v) if ($v === null) return $k;
        return false;
    }
    any_null(array('x' => $x, 'y' => $y));
    

    Note the strict comparison (===). Otherwise false, "", 0 and array() would be counted as “null” as well.

    Another solution is to only pass the name of your variables and make use of the $GLOBALS array. Here I’ve coupled it with a call to func_get_args to get a little less verbose calling convention:

    function any_null(){
        $a = func_get_args();
        foreach($a as $k) if (!isset($GLOBALS[$k])) return $k;
        return false;
    }
    any_null('x', 'y', 'z');
    

    This last solution have a lot of shortcomings though. You can only test variables in the global scope, and you can only test standard variables (not elements in an array, not objects and so on). The syntax for those would be very clunky.

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

Sidebar

Related Questions

Is there any other shorter/more efficient way to check and see if it is
Is there any time in which a reference type is more efficient than a
s/(?P<head>\[\[foo[^\[]*)abc/\g<head>def s/(?=\[\[foo[^\[]*)abc/def Which is more efficient? Are there any other ways to make it
I know and use eric meyer CSS reset, but is there any more things
Are there any scenarios where more than one Box2D worlds are needed in one
Is there any way to connect more then one PPC to ActiveSync in one
Are there any cases in which anything more than a linear speed increase comes
Is there any possible way to apply more than CSS to a control through
Is there any advantage or where returning a iterator is more desirable that a
Is there any other way to write javascript:false that is more pleasant? I'm building

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.