Home >Backend Development >PHP Tutorial >PHP array_pop()
The array_pop() function of the PHP Programming Language is an inbuilt function and it is the most helpful tool which helps in deleting or popping out and to returns the last element of the array and then it is passed to its specific parameter. Then it helps in reducing the array size by one value since only the last element is removed from the specified array. It the most helpful parameter when there is a need in using arrays in the PHP Programming Language. The array_pop() function usually resets the reset() which is the array pointer of the specific input array after usage.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax and parameters:
array_pop($array1)
Parameters
Explanation of the Parameters of the array_pop() function:
There is only one parameter which lies inside of the array_pop() function. It is a variable parameter “$array1”.
$array1 parameter of the array_pop() function: The $array1 parameter of the array_pop() function is an input array value and it helps in popping out the last element from the function and reduces the array size by one value.
The return value of array_pop() function: This array_pop() function of the PHP language helps in returning the last element of the specified array. If the array is an empty one or the input parameter is having non-array value, then the NULL value will be returned.
The array_pop() function of the PHP Programming Language works just by deleting of popping out the last element of the specific array passed to it as a parameter. It works just by reducing the length of the specific array to only one value. This array_pop() function of PHP only works for PHP 4, PHP 5 and PHP 7 versions. This built-in function works just by popping the last element of the array from the end.
Below are the examples mentioned:
This is the example of implementing the array_pop() function of the PHP Programming Language to handle the array/arrays in a specific way but here only one array is used. Here at first array element variable “$array1” is used to store some string values with index value with the help of array() function. Print_r is used just to display the content present in the array. It displays array content along with the index values of the array. Then array_pop($array1) is used to delete the last element present in the “$array1” array. The array_pop() will remove the last element from the array. Then print_r() is used to print the remaining elements present in the array. It will print the array values along with the index values of those values. Likewise here three times, array_pop() is used to pop out elements 3 times.
is used for line breaks and “
Code:
<?php // This is the sample PHP code of illustrating the array1_pop() usage $array1 = array(1=>"ram", 2=>"krishna", 3=>"aakash"); echo "The array1 values :: "; echo "<br>"; print_r($array1); echo "<br>"; echo "<hr>"; print_r("Popped element is :: "); echo array_pop($array1); echo "<br>"; echo "<hr>"; print_r("\nIt is just After popping the array last element, " "the array1 reduces to: \n"); echo "<br>"; print_r($array1); echo "<br>"; echo "<hr>"; echo "<hr>"; print_r("Second time Popped element is :: "); echo array_pop($array1); echo "<br>"; echo "<hr>"; print_r($array1); echo "<br>"; echo "<hr>"; echo "<hr>"; print_r("Third time Popped element is :: "); echo array_pop($array1); echo "<br>"; echo "<hr>"; print_r($array1); ?>
Output:
This example is also similar to the above PHP program but here the array values are only integer values. Whatever might be the values, array_pop() acts the same. Here “$pavansake_array1” is used to store array elements with the help of the array() function of the PHP language. The echo with “
” is generally used for line breaks and with “
Code:
<?php // This is the sample PHP code of illustrating the array_pop() usage for only integer values $pavansake_array1 = array(3, 19, 22, 28, 4, 20); echo "The input values of pavansake_array1 array values :: "; echo "<br>"; print_r($pavansake_array1); echo "<br>"; echo "<hr>"; print_r("Popped element is :: "); echo array_pop($pavansake_array1); echo "<br>"; echo "<hr>"; print_r("\nIt is just After popping the array last element, ". "the pavansake_array1 reduces to: \n"); echo "<br>"; print_r($pavansake_array1); echo "<br>"; echo "<hr>"; echo "<hr>"; print_r("Second time Popped element is :: "); echo array_pop($pavansake_array1); echo "<br>"; echo "<hr>"; print_r($pavansake_array1); echo "<br>"; echo "<hr>"; echo "<hr>"; print_r("Third time Popped element is :: "); echo array_pop($pavansake_array1); echo "<br>"; echo "<hr>"; print_r($pavansake_array1); ?>
Output:
This is also a similar example of array_pop() function just like above but here in order to show the array values, I used “for each” functionality. It is used just to print the array values present at a specific instance. Other than that, everything in the code is the same/similar to the above example’s codes.
Code:
<?php // This is the sample PHP code of illustrating the array_pop() usage for only integer values $pavansake_array1 = array(3, 19, 22, 28, 4, 20); echo "The input values of pavansake_array1 array values :: "; echo "<br>"; foreach($pavansake_array1 as $values){ echo "$values,"; } echo "<br>"; echo "<hr>"; print_r("Popped element is :: "); echo array_pop($pavansake_array1); echo "<br>"; echo "<hr>"; print_r("\nIt is just After popping the array last element, ". "the pavansake_array1 reduces to: \n"); echo "<br>"; echo "Now the array result is :: <br>"; foreach($pavansake_array1 as $values){ echo "$values,"; } echo "<br>"; echo "<hr>"; echo "<hr>"; print_r("Second time Popped element is :: "); echo array_pop($pavansake_array1); echo "<br>"; echo "<hr>"; echo "Now the array result is :: <br>"; foreach($pavansake_array1 as $values){ echo "$values,"; } echo "<br>"; echo "<hr>"; echo "<hr>"; print_r("Third time Popped element is :: "); echo array_pop($pavansake_array1); echo "<br>"; echo "<hr>"; echo "Now the array result is :: <br>"; foreach($pavansake_array1 as $values){ echo "$values,"; } ?>
Output:
I hope you learned what is the definition of PHP array_pop() along with its syntax and the explanation of the parameters, How the array_pop() function works in PHP Programming Language along with various examples of the array_pop() function concept of PHP language.
The above is the detailed content of PHP array_pop(). For more information, please follow other related articles on the PHP Chinese website!