Wednesday, March 18, 2009

array_pop function in PHP

array_pop():
array_pop() pops and returns the last value of the array , shortening the array by one element. If array is empty (or is not an array), NULL will be returned. Will additionally produce a Warning when called on a non-array.

Note:
This function will reset() the array pointer after use.

Example:

<?php
$stack = array("fruits", "cars", "animals", "birds");
$category= array_pop($stack);
print_r($stack);
?>

When this file is executed the output will be

Array
(
[0] => fruits
[1] => cars
[2] => animals

)

Here only the 3 elements will be viewed in the array and the last element is assigned to the variable.

The value of the category will be "birds".

0 comments:

Post a Comment