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

The Archive Base Latest Questions

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

I sometimes have variables that might not be set and I would like to

  • 0

I sometimes have variables that might not be set and I would like to use a default parameter instead. Like here:

if ($p == "a") doSomething();

If $p is not defined PHP throws Notice: Undefined variable. To avoid this I often I used this construct in such a case:

$p = (isset($p) ? $p : "");

But that is ugly if you have to use it a lot. So I wrote a function for it:

function getIfSet(&$value, $default = '')
{
    return isset($value) ? $value : $default;
}

// Example
if (getIfSet($p) == "a") doSomething();

I wonder if there is a PHP function for this or how you solve this.

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

    I tried to make renocor’s answer more clean and OOP when I came up with this solution:

    class NiceArray implements ArrayAccess {
        protected $array;
    
        public function __construct(&$array) {
            $this->array =& $array;
        }
    
        public function offsetExists($offset) {
            return true;
        }
    
        public function offsetGet($offset) {
            if (isset($this->array[$offset]))
            {
                return $this->array[$offset];
            }
            else
            {
                return null;
            }
        }
    
        public function offsetSet($offset, $value) {
            $this->array[$offset] = $value;
        }
    
        public function offsetUnset($offset) {
            unset($this->array[$offset]);
        }
    }
    

    Usage:

    $get = new NiceArray($_GET);
    if ($get['p'] == "a") doSomething();
    

    I know the class is kind of big but this way you still have an array and you can easily use it for every array you want. You do not need to change any code you may had before. You can still access and change the data. It will even change the original array.

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

Sidebar

Related Questions

I have noticed that sometimes people have to use multiple versions of jQuery in
This might not be possible but it would be hugely helpful with debugging. Sometimes
I have a variable that contains : Sometimes array of strings and sometimes array
I see on sites that they sometimes have a statistic showing how many views
Sometimes I have to check for some condition that doesn't change inside a loop,
Sometimes I have to write code that alternates between doing things and checking for
I have a Haskell script that runs via a shebang line making use of
I have a bunch of variables that are equal to values pulled from a
Say I have a set of data points that are represented as an array
Our build workflows are a bit involved in the sense they sometimes have 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.