Difference Between Static Array And Dynamic Array In PHP

admin_img Posted By Bajarangi soft , Posted On 02-09-2020

Difference between array and dynamic array in PHP

dynamic_array

Different between array and dynamic array in PHP

  • Static arrays, sometimes simply called arrays, are allocated with a fixed size.
  •  Dynamic arrays increase their size when an insertion occurs and the array currently has no space for the new value.Usually the area doubles in size.
  • Static arrays are allocated memory at compile time and the memory is allocated on the stack.
  • The dynamic arrays are allocated memory at the runtime and the memory is allocated from heap.

1.Static arrays

Static variable initialized in constant. So while you may initialize a static property to an integer or array (for instance), you may not initialize it to another variable, to a function return value, or to an object.
 
class static_array {
    public $bar = "BIKE";
}

class myClass {
    static public $array = array();
    static public function init() {
        while ( count( self::$array ) < 3 )
            array_push( self::$array, new static_array() );
    }
}

myClass::init();
print_r( myClass::$array );

2. Dynamic arrays

Dynamic array is a special variable, which can hold more than one value at a time.
 
$color = array("blue","black","white","red","green");
print_r($color);

Complete Code of Different between array and dynamic array in PHP:
 
<html>
<head>
    <title>Different between array and dynamic array in PHP</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container ">
    <div class="text-center">
        <h1>Different between Array and Dynamic array in PHP</h1>
    </div>
    <br>
    <h3>Array</h3>
    <div class="well">
        <?php

        class static_array {
            public $bar = "BIKE";
        }

        class myClass {
            static public $array = array();
            static public function init() {
                while ( count( self::$array ) < 3 )
                    array_push( self::$array, new static_array() );
            }
        }
        myClass::init();

        print_r(  myClass::$array );

        ?>
    </div>
    <h3>Dynamic Array</h3>
    <div class="well">
        <?php
        $color = array("blue","black","white","red","green");
        print_r($color);
        ?>
    </div>
</div>
</body>
</html>

Related Post