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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:46:52+00:00 2026-05-13T14:46:52+00:00

I have a few functions that should return an array, to have it merged

  • 0

I have a few functions that should return an array, to have it merged with another array.

However, sometimes there’s nothing to return.
What’s the best scenario here?

  1. Return an empty array and merge that with the full one OR
  2. Return null, store the return in a variable, check that variable and THEN merge it if needed.

I’m asking this because sometimes the shortest route isn’t the fastest, and I don’t really know what array_merge() does under the hood.

  • 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-13T14:46:53+00:00Added an answer on May 13, 2026 at 2:46 pm

    Return the empty array. Compare the complexity of your two options

    1. Return an empty array and merge that with the full one OR
    2. Return null, store the return in a variable, check that variable and THEN merge it if needed.

    When you write a function, people other than you are going to use it (this includes the you from 6 month forward who has no idea what current you is doing). If you return null, someone using your function needs to know it might not return an array, so anytime they use your function then need to wrap their variables in a lot of is_array or is_set checks. This leads to harder to maintain code down the road, or bugs where it works when your app/system works when an array is returned, but not when a null is returned. If your function always returns an array, people can safely pass it to functions expecting an array. (this is why some advocates of strong type enforcement hate PHP. In a language like Java this doesn’t happy because functions have return a specific type of thing)

    You attention to performance is laudable, but in general the built in array manipulation functions are pretty well tuned, and are only going to bottleneck a small percentage of the time. In day to day code running, the performance benefits of checking the value of the variables and merging in a zero element array are going to be negligible.

    Go with the cleaner API, benchmark, and then optimize specific cases where you start seeing a performance problem.

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

Sidebar

Ask A Question

Stats

  • Questions 285k
  • Answers 285k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You probably won't use many of the files in Gdata… May 13, 2026 at 4:39 pm
  • Editorial Team
    Editorial Team added an answer I would suggest looking at jMeter & the jMeter Ant… May 13, 2026 at 4:39 pm
  • Editorial Team
    Editorial Team added an answer Yes, it is necessary, if you want all of them… May 13, 2026 at 4:39 pm

Related Questions

I have a data structure that I want to access / modify in different
I was wondering how i should go about writing an XML data layer for
The following code is from a tutorial ( http://net.tutsplus.com/php/creating-a-php5-framework-part-1/ ), not mine. I have
Sadly, I have a legacy PHP4 system that I continuously need to add features
I'm trying to use an abstract class when passing an extended object as an

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.