PHP 8 reference · Array Functions

PHP array_shift() Function

Remove the first element of an array and return it. Numeric keys are renumbered from 0; string keys stay.

PHP 8 Since 4.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

array_shift(array &$array): mixed

Parameters

ParameterDescription
array The array (changed in place).

Return value

The removed value, or null if empty.

Examples

Take from the frontTry it yourself
<?php
$queue = ["first", "second", "third"];
echo array_shift($queue), "\n";
print_r($queue);

Output

first
Array
(
    [0] => second
    [1] => third
)
Drop the script nameTry it yourself
<?php
$args = ["script.php", "--verbose", "file.txt"];
$script = array_shift($args);
echo "$script got ", count($args), " arguments\n";

Output

script.php got 2 arguments

Version history

VersionChange
PHP 4.0Added

In other languages

Was this page helpful?