Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

array_pad()

Example

Return 5 elements, inserting the value “blue” into the new positions in the array:

<?php
$a=array(“red”,“green”);
print_r(array_pad($a,5,“blue”));
?>

Definition and Usage

The array_pad() function adds a specified number of elements with a given value to an array.

Tip: If a negative size parameter is used, the function will insert new elements before the original elements (see example below).

Note: This function does not remove any elements if the size parameter is less than the size of the original array.

Syntax

array_pad(array, size, value)

Parameter Values

Parameter

Description

array

Required. Defines an array.

size

Required. Specifies the number of elements in the array returned by the function.

value

Required. Specifies the value for the new elements in the array returned by the function.

Technical Details

Return Value:

Returns an array with added elements.

PHP Version:

4+

More Examples

Example

Using a negative size value:

<?php
$a=array(“red”,“green”);
print_r(array_pad($a,-5,“blue”));
?>