Aim
To implement the PHP array functions array_pad(), array_slice() and array_splice() together with list() destructuring, displaying every result with foreach wherever applicable.
Theory
PHP arrays are ordered maps, and the standard library manipulates them through value-returning and in-place functions — knowing which is which is the heart of this practical:
array_pad($array, $size, $value)returns a copy grown to$sizeelements by appending$valueas many times as needed. A negative size pads at the front instead, and if the array already has at least|$size|elements it is returned unchanged — the function never truncates.array_slice($array, $offset, $length)extracts a contiguous window without modifying the source — it is a pure read. A negative$offsetcounts from the end, and numeric keys are re-indexed from 0 unless the fourth argumentpreserve_keysistrue.array_splice(&$array, $offset, $length, $replacement)is the destructive sibling: the array is passed by reference, the selected segment is removed and returned, and the$replacementelements are stitched into the gap (the replacement may be shorter or longer than what was removed, so the array can shrink or grow). Numeric keys are renumbered afterwards.list($a, $b, $c) = $array(short form[$a, $b, $c] = $array) destructures an array into individual variables in one statement — the same mechanism P6 used for swapping. Elements can be skipped with empty slots, and since PHP 7.1 keyed destructuring["x" => $x]works too.
The mnemonic examiners love: slice reads, splice edits.
Requirements
- XAMPP/WAMP with PHP 8.x, or PHP CLI
- Code editor (VS Code); browser or terminal
Procedure
- Start Apache from the XAMPP Control Panel.
- Save the snippet as
p07_array_functions.phpinC:\xampp\htdocs\wbplab. - Run
http://localhost/wbplab/p07_array_functions.phporphp p07_array_functions.php. - After the first run,
print_r($subjects)at the end to provearray_pad/array_sliceleft it untouched, thenprint_r($spliceSource)to provearray_splicemutated it. - Try
array_pad($subjects, -6, "N/A")and a negative slice offset, predicting each result first.
Explanation of the Code
$subjects = ["PHP", "HTML", "CSS"]is the base array.array_pad($subjects, 6, "N/A")returns a six-element copy in$padded— the three originals plus three"N/A"fillers;$subjectsitself is untouched.array_slice($padded, 1, 3)copies three elements starting at index 1 into$sliced:HTML,CSSand the firstN/A.$spliceSource = ["A", "B", "C", "D", "E"];array_splice($spliceSource, 2, 2, ["X", "Y"])removes two elements at index 2 (C,D) — those come back as the return value in$removed— and insertsX,Yin their place, mutating$spliceSourcetoA, B, X, Y, E.list($first, $second, $third) = $subjects;unpacks the original array into three scalars.- Four
foreachloops print each result one value per line; the finalechointerpolates the three destructured variables.
Expected Output
The script prints five labelled blocks: array_pad result lists PHP, HTML, CSS, N/A, N/A, N/A (one per line); array_slice result lists HTML, CSS, N/A; array_splice removed lists C, D; array_splice updated source lists A, B, X, Y, E; and the last line reads list() values: PHP, HTML, CSS.
🎯 Viva Questions
- Does
array_pad()ever shorten an array? No — if the array already has at least the requested size it is returned unchanged. - How do you pad at the beginning instead of the end? Pass a negative size:
array_pad($arr, -6, "N/A"). - Key difference between
array_slice()andarray_splice()? Slice returns a copy and leaves the source alone; splice takes the array by reference, removes a segment and edits in place. - What does
array_splice()return? An array containing exactly the elements it removed. - Can the replacement in
array_splice()differ in length from the removed part? Yes — the array grows or shrinks accordingly, then numeric keys are renumbered. - What is the short-syntax equivalent of
list()? Square-bracket destructuring:[$first, $second, $third] = $subjects;(PHP 7.1+).
CO Mapping
CO1, CO2