array_pop
array_pop(array &$array): mixed
Pops and returns the last element of the array, shortening the array by one element.
C C23
#include <stdio.h>
int main(void) {
const char *stack[] = {"a", "b", "c"};
int size = 3;
const char *last = stack[--size];
printf("%s\n", last);
printf("size=%d\n", size);
return 0;
}
c size=2
C++ C++26
#include <iostream>
#include <vector>
int main() {
std::vector<std::string> stack{"a", "b", "c"};
std::string last = stack.back();
stack.pop_back();
std::cout << last << "\n";
std::cout << stack.size() << "\n";
return 0;
}
c 2
C# v11.0.100
var stack = new List<string> { "a", "b", "c" };
string last = stack[^1];
stack.RemoveAt(stack.Count - 1);
Console.WriteLine(last);
Console.WriteLine(stack.Count);
c 2
Go v1.27.4
package main
import "fmt"
func main() {
stack := []string{"a", "b", "c"}
last := stack[len(stack)-1]
stack = stack[:len(stack)-1]
fmt.Println(last)
fmt.Println(len(stack))
}
c 2
Java v27.0.1
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> stack = new ArrayList<>(List.of("a", "b", "c"));
String last = stack.remove(stack.size() - 1);
System.out.println(last);
System.out.println(stack.size());
}
}
c 2
JavaScript v26.13.1
const stack = ["a", "b", "c"];
const last = stack.pop();
console.log(last, stack);
c [ 'a', 'b' ]
PHP v8.6.0
$stack = ["a", "b", "c"];
$last = array_pop($stack);
echo $last, "\n", json_encode($stack);
c ["a","b"]
Python v3.15.2
stack = ["a", "b", "c"]
last = stack.pop()
print(last, stack)
c ['a', 'b']
Rust v1.99.1
fn main() {
let mut stack = vec!["a", "b", "c"];
let last = stack.pop().unwrap();
println!("{} {:?}", last, stack);
}
c ["a", "b"]
TypeScript v26.13.1
const stack: string[] = ["a", "b", "c"];
const last: string | undefined = stack.pop();
console.log(last, stack);
c [ 'a', 'b' ]
Language differences
C: no built-in pop — decrementing a --size counter is enough, no data is actually erased.
C++: no built-in pop — split it into back() to read plus pop_back() to remove (pop_back returns nothing).
C#: no dedicated pop — idiomatic is [^1] plus RemoveAt(Count-1), or a Stack<T>.
Go: no built-in pop — reslice with s[:len(s)-1] (the backing array keeps the memory, so lingering references block GC).
JavaScript: pop() returns undefined.
PHP: all languages agree on the semantics — take the last element and shrink by one; the differences surface on empty input: array_pop([]) returns null and emits a warning.
Python: pop() raises IndexError.
Rust: Vec::pop expresses it with Option<T> (empty yields None, enforced by the type system).
TypeScript: same as JavaScript — pop() returns undefined.