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

The Archive Base Latest Questions

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

Basically in the case of working with large arrays it’s convenient to pass null

  • 0

Basically in the case of working with large arrays it’s convenient to pass null back if an error occurs since if $array = null then $array[] = 1 is [ 1 ] and null is also usable in a callable context, ie. function (array $array = null) will accept null as an acceptable value. Basically null is convenient since you can easily identify it as an error if you have error handling code, but also easily ignore it if you don’t care.

This is fairly straight forward in most cases, however there is a corner case where PHP doesn’t really support it all that well and that’s when passing back a reference in the context of a function that doesn’t necessarily accept a reference but needs to pass null back sometimes, yet pass a reference back other times (most of the time this is not an issue since you’re returning a reference to a instance variable but sometimes that’s not the case). There’s also the case of calling a function with a null value in a non awkward way.

The reason to pass the reference is obviously to save the trouble of copying the array around (especially when it’s very large).

The following “solution”…

\error_reporting(-1);

function & nil()
{
    $nil = null;
    return $nil;
}

function & pass(array & $variable = null)
{
    return $variable;
}

function & check ()
{
    return nil();
}

$test = pass(nil());
$test = &pass(nil());

$test1 = &check();
$test1[] = 1;
$test2 = &check();
$test2[] = 2;

\var_dump($test1, $test2);

Works, but…

My question is Does PHP guarantee the local variable won’t be garbage collected before all references to it are garbage collected? or is that undefined behavior.

  • 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-18T03:59:38+00:00Added an answer on June 18, 2026 at 3:59 am

    PHP will never GC anything while reference count is greater than 0.

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

Sidebar

Related Questions

I am working with vImages in Cocoa Touch, which in my case are basically
So far I have this case statement working where I basically assign a value
the jQuery tablesorter plugin isn't working too well for one specific case. Basically, I
Basically, I want to generate an item, in this case, a ring. I have
Basically I have a large set of data in excel, and I was wondering
Basically, I'm still working on a puzzle-related website (micro-site really), and I'm making a
I'm working on a project for school. This is the case: You should be
(GridWorld as in Collegeboard's AP exam case study thing) So, I've been working on
I am working in a mailing application in C# and, basically, I need to
I have a relatively simple case. I basically want to store data about links

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.