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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:01:05+00:00 2026-05-16T11:01:05+00:00

I use the ternary operator quite often but I’ve not been able to find

  • 0

I use the ternary operator quite often but I’ve not been able to find anything in the documentation about this and I’ve always wondered it.

The following is a possible example:

echo ($something->message ? $something->message : 'no message');

as you can see, if $something->message is correct, we return $something->message, but why write it twice? Is there a way to do something like:

echo ($something->message ? this : 'no message');

Now I’m not well versed in programming theory, so it’s possible that there is a reason that the former cannot be referenced with something like “this” but why not? Would this not stream line the use of the ternary operator? For something like my example it’s pretty useless, but let’s say it’s

echo (function(another_function($variable)) ? function(another_function($variable)) : 'false');

I’m unable to find any way to do this, so I’m assuming it’s not possible, if I’m wrong please inform me, otherwise: why not? Why is this not possible, what’s the technical reason, or is it just something that never happened? Should I be declaring it as a variable and then testing against that variable?

  • 1 1 Answer
  • 3 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-16T11:01:06+00:00Added an answer on May 16, 2026 at 11:01 am

    Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.

    Source

    For example

    $used_value = function1() ?: $default_value;
    

    Is the same as

    $check_value = function1(); //doesn't re-evaluate function1()
    if( $check_value ) {
        $used_value = $check_value;
    } else {
        $used_value = $default_value;
    }
    

    Word for the wise

    If your going to be depending on typecasting to TRUE it’s important to understand what WILL typecast to TRUE and what won’t. It’s probably worth brushing up on PHP’s type juggling and reading the type conversion tables. For example (bool)array() is FALSE.

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

Sidebar

Related Questions

I like judicious use of the ternary, conditional operator. To my mind it's quite
How to use ternary operator with System::Boolean? This sample code always returns true: bool
I was asked to perform this operation of ternary operator use: $test='one'; echo $test
After reading To ternary or not to ternary? and Is this a reasonable use
I use ternary operators alot but I can't seem to stack multiple ternary operator
I'm trying to use a ternary operator in Razor, similar to this question ,
Why can't we use return keyword inside ternary operators in C, like this: sum
use this website a lot but first time posting. My program creates a number
I’m trying to shorten my code using the ternary operator. This is my original
So I ran into something interesting that I didn't realize about the ternary operator

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.