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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:26:26+00:00 2026-05-26T17:26:26+00:00

Is it possible to use the shorthand ternary to check whether a variable is

  • 0

Is it possible to use the shorthand ternary to check whether a variable is set or not instead of whether is evaluates to zero or non-zero?

For example, I tried:

$var = 0;
echo (string) $var ?: (string) false ?: 2;

But since both the first two expressions evaluate to "0" or "false", 2 is displayed.

I thought that perhaps casting them to a string would produce different results, but it did not. Zero is zero I suppose.

I’m wanting to use this style when assigning variables such as

$get->var = $get->var ?: $setindb ?: $default;

I want to assign $get->var to $get->var if it is set, otherwise, check if the db has a value, otherwise, use a default.

Edit

I thought I would mention that I know I could do something like

$get->var = (!empty($get->var)) ? $get->var : ( (!empty($setindb)) ? $setindb : $default )

But you be the judge at which is simpler 🙂

  • 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-26T17:26:27+00:00Added an answer on May 26, 2026 at 5:26 pm

    The string “0” and “false” are considered FALSE-y (list of false values) values in PHP.

    isset() returns TRUE for variables that are set and not NULL. empty() will check that the variable is set and that is isn’t a FALSE value. So it would return TRUE for “0”.

    I think that what you want is your third code snippet, but with isset(), rather than empty().

    The easiest way to do (with the least amount of brackets) that would be:

    $get->var = isset($get->var) ? $get->var : (isset($setindb) ? $setindb : $default);
    

    Since the shorthand tenary returns the first parameter, you can’t use it, because that would be a boolean. ( isset($get->var) ?: $default would return TRUE, FALSE or $default, but never $get->var‘s value. )


    Edit: Perhaps you’d like something like a coalesce function? PHP doesn’t offer it natively, as far as I know, but it’s trivial to create. Note that this might be a bit ugly, due to its use of passing references to a function. I’d be interested in comments on this (is it as bad as I think it is?).

    function coalesce(array $values)
    {
        foreach($values as &$value)
        {
            if(isset($value))
                return $value;
        }
        return null;
    }
    $this->var = coalesce(array(&$this->var, &$setindb, $default));
    

    I haven’t tested the above. Pass variables in the array as references (& symbol). I’m pretty sure it’ll choke on something like coalesce(array(1,"a_string",false));.

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

Sidebar

Related Questions

Possible Duplicate: <? ?> tags not working in php 5.3.1 I usually use shorthand
Is it possible to use gcov for coverage testing of multi-threaded applications? I've set
what could be a possible use of declaring types within a namespace but not
Is it possible use in-explicit constructing with operators ? Just like in this example
Is it possible use a MySQL query to perform this kind of check? If
Is it possible use mod_rewrite to resolve addresses hosted on another server? Say I
I have recently started looking into Google Charts API for possible use within the
Possible Duplicate: Use SVN Revision to label build in CCNET I'm working through the
Is possible to use ArcSDE API in .NET(C#) ? http://edndoc.esri.com/arcsde/9.2/api/capi/dbconnects/dbconnects.htm Thanks
Is it possible to use a flash document embedded in HTML as a link?

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.