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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:18:59+00:00 2026-06-12T12:18:59+00:00

This code works: $foo = getFoo(); if (!$foo) $foo = getBar(); if (!$foo) $foo

  • 0

This code works:

$foo = getFoo();
if (!$foo) $foo = getBar();
if (!$foo) $foo = getJiggy();
if (!$foo) $foo = getWithIt();

I thought I’d seen somewhere a simplification of it with logical operators:

$foo = (getFoo() || getBar() || getJiggy() || ...);

I figured that the first true statement would get passed, but instead, it’s just setting $foo to boolean true instead of the return value of getFoo(), getBar(), etc.

Is there a simplification like what I’m thinking of?

  • 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-12T12:19:01+00:00Added an answer on June 12, 2026 at 12:19 pm

    For JavaScript, foo = bar || baz; is a commonly used expression, as the || operator has a coalescing behavior.

    PHP does not have this behavior with regard to the || operator, which returns a boolean value. As such, the more verbose code you originally posted:

    $foo = getFoo();
    if (!$foo) $foo = getBar();
    if (!$foo) $foo = getJiggy();
    if (!$foo) $foo = getWithIt();
    

    is your most readable, and preferable option.

    PHP 5.3 has a shorthand version of the ternary operator, which acts as a coalescing operator:

    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.

    This would allow you to use:

    $foo = getFoo() ?: getBar() ?: getJiggy() ?: getWithIt();
    

    However, that assumes you don’t have to worry about compatibility.

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

Sidebar

Related Questions

Here's some working code: class A { public $Foo; public function GetFoo() { $this->Foo
Imagine this code: class foo { foo fff; int m_id; f & work() {
This code works well: UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:pickerFrame]; [pickerView addTarget:self action:@selector(pickerChanged:) forControlEvents:UIControlEventValueChanged];
This code works fine in Chrome. However, in Firefox, when it hits the GMxhr
This code works for me except it only checks for a duplicate once, and
This code works well for displaying separate xml items in my main.xml. But is
This code works fine, but I'll want to handle exception if any thing goes
This code works: monkey.h @interface monkey : NSObject { NSNumber *monkeyRanch; } @property (nonatomic,
This code works (when ControlType=dropDown then the background yellow ): <Window x:Class=TestCollapsed.Views.MainView xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
this code works fine until I start scrolling: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

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.