Friday, December 12, 2025
HomeLanguagesPHP | SplFixedArray __construct() Function

PHP | SplFixedArray __construct() Function

The SplFixedArray::__construct() function is an inbuilt function in PHP which is used to construct a new fixed size array.

Syntax:

void SplFixedArray::__construct( $size )

Parameters: This function accepts single parameter $size which specifies the size of an array.

Return Value: This function does not return any value.

Below programs illustrate the SplFixedArray::__construct() function in PHP:

Program 1:




<?php
  
// Create new fixed array of size 2
$gfg = new SplFixedArray(2);
  
$gfg[1] = "neveropen";
  
// Print Result 
var_dump($gfg[0]);
var_dump($gfg[1]);
  
?>


Output:

NULL
string(13) "neveropen"

Program 2:




<?php
  
// Create new fixed array of size 8
$gfg = new SplFixedArray(8);
  
$gfg[2] = 5;
$gfg[4] = "gfg";
$gfg[5] = "Geeks";
$gfg[7] = "neveropen";
  
// Iterate array and print its values
foreach( $gfg as $i ) {
    var_dump($i);
}
?>


Output:

NULL
NULL
int(5)
NULL
string(3) "gfg"
string(5) "Geeks"
NULL
string(13) "neveropen"

Reference: https://www.php.net/manual/en/splfixedarray.construct.php

RELATED ARTICLES

Most Popular

Dominic
32445 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6814 POSTS0 COMMENTS
Nicole Veronica
11952 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12029 POSTS0 COMMENTS
Shaida Kate Naidoo
6949 POSTS0 COMMENTS
Ted Musemwa
7199 POSTS0 COMMENTS
Thapelo Manthata
6895 POSTS0 COMMENTS
Umr Jansen
6882 POSTS0 COMMENTS