PHP Tutorial - PHP array_pad() Function






Definition

The array_pad() function inserts value to an array element.

A negative size means to insert before the array elements.

Syntax

PHP array_pad() Function has the following syntax.

array_pad(array,size,value)

Parameter

ParameterIs RequiredDescription
arrayRequired.Array to insert
sizeRequired.Number of elements to return
valueRequired.Value to insert

Example 1

Return 5 elements and insert a value of "blue" to the new elements in the array:


<?php
   $a=array("A","B");
   print_r(array_pad($a,5,"C"));
?>

The code above generates the following result.





Example 2

Using a negative size parameter:


<?php
    $a=array("A","B");
    print_r(array_pad($a,-5,"C"));
?>

The code above generates the following result.