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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:08:07+00:00 2026-05-25T18:08:07+00:00

Earlier today I tried to do this: Example 1: <?php echo $myVar || rawr;

  • 0

Earlier today I tried to do this:

Example 1:

<?php 

  echo $myVar || "rawr";

?>

I thought this might print out $myVar if it was set, and print "rawr" if not.
It printed a 1, I assume this is the result of the OR test.

What I then tried was this:

Example 2:

<?php
  if ($myVar)
  {
    echo $myVar;
  }
  else
  {
    echo "rawr";
  }
?>

Which is what I was trying to accomplish.

I think I understand why the first prints the results of the OR test rather than one of the variables, and also why I tried it – been spending some time on the bash shell recently 🙂

Can anyone tell me if there is a way to perform the text in example #2 but in similar syntax to example #1?

  • 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-25T18:08:08+00:00Added an answer on May 25, 2026 at 6:08 pm

    PHP is notoriously inelegant in this department, and there really is no good way of making this test.

    As a general solution, you ‘d need to use the ternary operator as in empty($var) ? "rawr" : $var. However, in practice what happens is that you have one of two scenarios:

    1. Your own variable

    In this case where you define the variable yourself, the best solution is to just give it a known default value at the place you define it (possibly with the ternary operator).

    2. Inside an array

    If the array is one that should not be touched like one of the superglobals, then you can wrap the test inside a function (pretty much that’s what everyone does).

    If the array is one under your jurisdiction but it comes from an external source, you can use the “add the defaults” trick:

    $incoming = array(...);
    $defaults = array("foo" => "bar");
    
    // Inject the defaults into $incoming without overwriting existing values
    $incoming += $defaults;
    

    At this point you know for a fact that every key inside $defaults also exists inside $incoming.

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

Sidebar

Related Questions

I tried this earlier today: public interface IFoo { IEnumerable<int> GetItems_A( ref int somethingElse
I tried to subclass threading.Condition earlier today but it didn't work out. Here is
I got some help with this earlier today but I cannot figure out the
earlier today I asked this question . So since moq creates it's own class
Earlier today I noticed some calls to php fwrite failing as the destination socket
This relates to another question I asked earlier today. I built SVN 1.6.2 from
Earlier today I asked this question . After spending some time investigating this issue,
Earlier today I tried to deprecate an old SQL Login by opening the Login's
OK, I asked this question earlier today How do I write a scope to
Okay this is a revised question from earlier today, I have included code to

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.