PHP provides two useful array manipulation functions: array_pop() and array_push(). These functions allow you to remove or add elements from the end of an array, making them ideal for stack-like operations and dynamic data handling.
array_pop() removes the last element of an arrayarray_push() adds one or more elements to the end
// Removes and returns the last element of an array
array_pop($array);
// Adds one or more elements to the end of an array
array_push($array, $value1, $value2);
Add items to the "stack" (push) or remove the top item (pop).
[]array_pop() removes the last element from an array and returns it.
// Removing the last fruit from the array
$fruits = array("Apple", "Banana", "Cherry");
$removed = array_pop($fruits);
echo $removed;
print_r($fruits);
The value Cherry is removed and stored in $removed. The remaining array contains Apple and Banana.
array_push() adds new elements to the end of an array.
// Adding new fruits to the array
$fruits = array("Apple", "Banana");
array_push($fruits, "Cherry", "Date");
print_r($fruits);
The array now contains four elements: Apple, Banana, Cherry, and Date.
Think of array_push() as push and array_pop() as pop in a stack data structure.
// Simulating stack operations
$stack = array();
array_push($stack, "A");
array_push($stack, "B");
array_push($stack, "C");
array_pop($stack);
print_r($stack);
array_pop()array_push() for adding multiple values efficiently