Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Practical 7: array_pad, array_slice, array_splice, list (with foreach)

Lesson 14 of 35 in the free Web Based Programming Lab notes on Siksha Sarovar, written by Rohit Jangra.

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 $size elements by appending $value as 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 $offset counts from the end, and numeric keys are re-indexed from 0 unless the fourth argument preserve_keys is true.
  • 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 $replacement elements 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

  1. Start Apache from the XAMPP Control Panel.
  2. Save the snippet as p07_array_functions.php in C:\xampp\htdocs\wbplab.
  3. Run http://localhost/wbplab/p07_array_functions.php or php p07_array_functions.php.
  4. After the first run, print_r($subjects) at the end to prove array_pad/array_slice left it untouched, then print_r($spliceSource) to prove array_splice mutated it.
  5. 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; $subjects itself is untouched.
  • array_slice($padded, 1, 3) copies three elements starting at index 1 into $sliced: HTML, CSS and the first N/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 inserts X, Y in their place, mutating $spliceSource to A, B, X, Y, E.
  • list($first, $second, $third) = $subjects; unpacks the original array into three scalars.
  • Four foreach loops print each result one value per line; the final echo interpolates 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

  1. Does array_pad() ever shorten an array? No — if the array already has at least the requested size it is returned unchanged.
  2. How do you pad at the beginning instead of the end? Pass a negative size: array_pad($arr, -6, "N/A").
  3. Key difference between array_slice() and array_splice()? Slice returns a copy and leaves the source alone; splice takes the array by reference, removes a segment and edits in place.
  4. What does array_splice() return? An array containing exactly the elements it removed.
  5. 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.
  6. What is the short-syntax equivalent of list()? Square-bracket destructuring: [$first, $second, $third] = $subjects; (PHP 7.1+).

CO Mapping

CO1, CO2