array_push
array_push(array &$array, mixed ...$values): int
Pushes one or more elements onto the end of the array and returns the new count (equivalent to $array[] = $value).
C C23
#include <stdio.h>
int main(void) {
const char *stack[8] = {"a"};
int size = 1;
stack[size++] = "b";
stack[size++] = "c";
for (int i = 0; i < size; i++) {
printf("%s\n", stack[i]);
}
printf("size=%d\n", size);
return 0;
}
a b c size=3
C++ C++26
#include <iostream>
#include <vector>
int main() {
std::vector<std::string> stack{"a"};
stack.push_back("b");
stack.push_back("c");
for (const auto &s : stack) std::cout << s << "\n";
std::cout << "size=" << stack.size() << "\n";
return 0;
}
a b c size=3
C# v11.0.100
var stack = new List<string> { "a" };
stack.Add("b");
stack.Add("c");
Console.WriteLine(string.Join("\n", stack));
Console.WriteLine($"count={stack.Count}");
a b c count=3
Go v1.27.4
package main
import "fmt"
func main() {
stack := []string{"a"}
stack = append(stack, "b", "c")
for _, s := range stack {
fmt.Println(s)
}
fmt.Printf("len=%d\n", len(stack))
}
a b c len=3
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"));
stack.add("b");
stack.add("c");
stack.forEach(System.out::println);
System.out.println("size=" + stack.size());
}
}
a b c size=3
JavaScript v26.13.1
const stack = ["a"];
const n = stack.push("b", "c");
console.log(stack, n);
[ 'a', 'b', 'c' ] 3
PHP v8.6.0
$stack = ["a"];
$n = array_push($stack, "b", "c");
echo json_encode($stack), "\n", $n;
["a","b","c"] 3
Python v3.15.2
stack = ["a"]
stack.append("b")
stack.append("c")
print(stack, len(stack))
['a', 'b', 'c'] 3
Rust v1.99.1
fn main() {
let mut stack = vec!["a"];
stack.push("b");
stack.push("c");
for s in &stack {
println!("{}", s);
}
println!("len={}", stack.len());
}
a b c len=3
TypeScript v26.13.1
const stack: string[] = ["a"];
const n = stack.push("b", "c");
console.log(stack, n);
[ 'a', 'b', 'c' ] 3
Language differences
C: arrays have a fixed size; the idiom is a hand-maintained size counter with stack[size++] = x, keeping capacity and length managed manually.
C++: push_back returns nothing useful.
C#: List.Add returns nothing useful.
Go: append may reallocate, so the return value must be reassigned: s = append(s, ...).
Java: List.add returns nothing useful.
JavaScript: push, like PHP, returns the new length.
PHP: array_push($arr, ...$v) accepts multiple values at once and returns the new count; for a single value the shorter $arr[] = $v is idiomatic.
Python: has no push — the counterpart is append (which returns None).
Rust: Vec::push returns nothing useful.
TypeScript: same as JavaScript — push returns the new length.