Example 1 — Pad at the End
Fill a short list up to five items with zeros.
<?php
$numbers = [1, 2];
$padded = array_pad($numbers, 5, 0);
echo implode(", ", $padded), PHP_EOL;
echo count($padded), PHP_EOL; How It Works
- Start with two items. The array holds 1 and 2.
- Ask for length 5. PHP needs three more items.
- Add the zeros. The value
0is appended three times. - Count the result. The array now has exactly five items.

