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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:54:12+00:00 2026-06-02T13:54:12+00:00

Suppose I want to have two variables and have them both equal to null

  • 0

Suppose I want to have two variables and have them both equal to null. (More realistically, I am thinking about an array that contains a large amount of nulls, but the “two variables” scenario is sufficient for the question.) Obviously, I can do this in more than one way. I can do this (method 1):

$a = null;
$b = $a;

By my understanding, the result of this is that there is one zval that is pointed to by two entries in the symbol table: 'a' and 'b'. But alternatively one might do this (method 2):

$a = null;
$b = null;

Naively one would expect that this should result in two different zvals, each pointed to by one entry in the symbol table.

Does it follow from this that if you want to have a large array, and many elements of the array will be null, it’s more efficient (in terms of zval/memory use) to create a $master_null variable with the value null, and then write the null elements of the array by assigning using $master_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-02T13:54:14+00:00Added an answer on June 2, 2026 at 1:54 pm

    Consider this script:

    $arr = array();
    for ($i = 0; $i < 100000; $i++) $arr[] = null;
    echo memory_get_usage() . "\n";
    

    which on my machine outputs: 21687696, that is 21 MB of used memory. On the other hand using this:

    $master_null = null;
    $arr = array();
    for ($i = 0; $i < 100000; $i++) $arr[] = $master_null;
    echo memory_get_usage() . "\n";
    

    outputs: 13686832, which is 13 MB. Based on this information, you can assume that at far as memory usage is your concern, it is actually better to indeed use the “master null” variable. However you still need to have all the items in the array, and every entry in a HashTable (internal representation of arrays) takes also some memory.

    If you want to dig deeper in the zvals and references, I suggest using the function debug_zval_dump. Using it, you can see, which variables share the same zval:

    $a = $b = $c = $d = "abc";
    debug_zval_dump($a);
    $x = $y = $z = $w = null; 
    debug_zval_dump($x);
    $q = null;
    debug_zval_dump($q);
    

    which outputs:

    string(3) "abc" refcount(5)
    NULL refcount(5)
    NULL refcount(2)
    

    And this implies that although variables $x and $q are both NULL, they are not the same zval. But $x and $y share the same zval, because they are assigned to each other. I believe you know of the function debug_zval_dump, but if not, make sure you carefully read the refcount explanation at http://php.net/manual/en/function.debug-zval-dump.php.

    Also at the end of my post, I want to say that this information might be useful for a better knowledge of PHP internals, I think it is quite useless to do any optimizations. Mostly because there are much better places to start optimizing scripts than such micro-optimizations. Also while this is not part of the specification, PHP authors might change this behaviour in the future (e.g. all NULL variables could share the same zval in some future version).

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

Sidebar

Related Questions

Suppose that you have two huge files (several GB) that you want to concatenate
Suppose I have two boolean variables, and I want to do completely different things
Suppose, I want to have a link or a button that when user click
Suppose you're building an HTML form and you want to have 2 or more
Suppose I have a Python class that I want to add an extra property
Suppose I have two p tags in the document. I want to call two
Suppose I have two fields F1 and F2. I want to update F1 to
Let's suppose that I have a list of elements, and I want to select
Let us suppose that I want to query mongo on the dateTime. I have
Suppose you have n processes, n > 2. You want to have agreement amongst

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.