php8.5
Home/ Manual/ spl / splfixedarray/ SplFixedArray::__construct

SplFixedArray::__construct

PHP function Edit on GitHub ✎

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

Constructs a new fixed array

Description

SplFixedArray::__construct(int $size = 0)

Initializes a fixed array with a number of null values equal to size.

Parameters

size

The size of the fixed array. This expects a number between 0 and PHP_INT_MAX.

Errors/Exceptions

Throws a ValueError when size is a negative integer.

Changelog

VersionDescription
8.0.0Now throws a ValueError if size is a negative; previously it threw a InvalidArgumentException.

Examples

SplFixedArray::__construct() example

php
<?php
$array = new SplFixedArray(5);

$array[1] = 2;
$array[4] = "foo";

foreach($array as $v) {
  var_dump($v);
}
?>

The above example will output:

output
NULL
int(2)
NULL
NULL
string(3) "foo"

Source: reference/spl/splfixedarray/construct.xml · from the official PHP manual (php/doc-en)