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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:43:01+00:00 2026-06-13T18:43:01+00:00

I have code that is used very extensively which fetches an array from another

  • 0

I have code that is used very extensively which fetches an array from another method, and sometimes returns the first element of that array. Given that null is an acceptable return value for the function, is it worth the performance overhead of calling isset() on the array index (or checking the array length, etc), or is it better to just return the non-existant index (warnings aside). What are the advantages of calling isset() aside from preventing the warning.

The example below is simplified, the real function doesn’t just get the first element of the array.

Return index which may not exist:

function get_array_element(){
    $array = get_array();       // function that returns array
    return $array[0];           // return index 0 which may not exist
}

Versus checking if index is set:

function get_array_element(){
    $array = get_array();       // function that returns array
    return (isset($array[0]))?  // check if index 0 isset() else return null
        $array[0] : 
        null; 
}
  • 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-13T18:43:02+00:00Added an answer on June 13, 2026 at 6:43 pm

    Throwing a notice when accessing an undefined index — in theory — should alert you for typos in key names. In practice, if you’re using isset first, you probably just copied the key name there. Or used a numeric index. Or a constant.

    On the other hand, in most cases you’re accessing an index withour caring wether it’s set or not — and in this scenario, using isset is just anoying. A lot of languages lets you just retrieve any index without warnings, Javascript for example: just returning an undefined.

    So I would advice to ignore the notice. Not all of them, because in some cases there really are helpful, so keep them turned on in development, but silence such array access by using @. Yes, it is ugly, but does its job:

    return @$array[0];
    

    Or in simple cases, maybe other solutions fit?

    return array_shift($array);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of generic code that is used a lot, which i'd
I have a very simple piece of code that I used in previous versions
I have some legacy code that was used to monitor my applications cpu,memory etc
I have a piece of old code that used a query using WQL to
So let's say I have a code checkout that I used --ignore-externals on originally.
I have a code base that has been used as an ASP.Net web application.
Here is the piece of code that I have used for Java 5.0 TreeSet<Integer>
I often see a pattern used in circumstances where we have look-up code that
We have a sub-project 'commonUtils' that has many generic code-snippets used across the parent
I have code that uses the BeautifulSoup library for parsing, but it is very

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.