PHP Array array_pop() Function (With Examples)

In this tutorial, we will learn about the PHP array_pop() function with its usage, syntax, parameters, return value, and examples. By IncludeHelp Last updated : December 31, 2023

PHP array_pop() function

The array_pop() function is used to delete/pop last element from the array.

Syntax

The syntax of the array_pop() function:

array_pop(array);

Parameters

The parameters of the array_pop() function:

  • array is the input array, function will delete last element from it.

Return Value

The return type of this method is mixed, it returns the value of the last element of array. If array is empty, null will be returned. [Source]

Sample Input/Output

Input:
//initial array is
$arr = array(100, 200, 300, 10, 20, 500);

//deleting elements
array_pop($arr);
array_pop($arr);
array_pop($arr);
   
Output:
Array
(
    [0] => 100
    [1] => 200
    [2] => 300
)

PHP array_pop() Function Example

PHP code to delete elements from an array using array_pop() function.

<?php
    $arr = array(100, 200, 300, 10, 20, 500);
    
    array_pop($arr);
    array_pop($arr);
    array_pop($arr);
    
    print("array after removing elements...\n");
    print_r($arr);
?>

Output

The output of the above example is:

array after removing elements...
Array
(
    [0] => 100
    [1] => 200
    [2] => 300
)

To understand the above example, you should have the basic knowledge of the following PHP topics:


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.