Skip to content

Use Of Array_pad() And Array_pop() Functions In PHP

In this write-up, you will be learning the use of array_pad() and array_pop() functions in php. Become part of our php training program and learn all other php array used in php development.

The function array_pad() is used to fill or add values to an input array to a given size.

Whereas function array_pop() is used to drop off the last element of the array which results in a reduced array in the output.

These two functions are described in detail in the below section.

ARRAY_PAD() FUNCTION

Syntax : array_pad($input_arr, $new_size, $value);

Three parameters are used in this function which are described below.

  • $input_arr- It represents the array of elements to be padded or filled with new values.
  • $new_size- It represents the size of the returned array after adding certain values. It should be noted that the value of the new size of the array should be greater than the length of the input array. Otherwise, no value will be added to the input array.
  • $value- It represents the value to be padded to the input array.

Result/ Return Value : On the successful execution of the php script, a new array of elements with the padded values will be returned. If the parameter $new_size is assigned a negative value, then the values are added in the beginning of the input array. If the parameter $new_size is assigned a positive value, then the values are added at the end of the input array.

php apad-1024x340

Example : We have shown below an example of array_pad() function. 

Output : The following output will be generated after running the above php script. Here, we have $arr as the input array parameter that has values red, green and yellow. We have then used array_pad() function to add one more array element i.e, blue to a given size of 6 to the input parameter. Since the size of the array is a positive value, so the element ‘blue’ will be added at the end of the input array as shown in the image below.

php-apadoutput-1024x365

ARRAY_POP() FUNCTION

Syntax : array_pop($arr);

A single parameter $arr is used in this function which represents the array of elements from which the last element is to be dropped.

Result/ Return Value : The successful execution of the php script will return the reduced array by removing the last element off the array. If the $arr is left empty, NULL will be returned.

Errors : If the parameter $arr is left empty, an E_WARNING error will be seen in the output screen.

php apop-1024x375

Example : An example of array_pop() function is shown below. 

php apopoutput-1024x314

Output : You will get the following output after the successful execution of the above php code. Here, variable $arr represents input parameter that has values red, green, blue, yellow and pink. We have then applied array_pop() function to the input array. Clearly the last element ‘pink’ is removed from the array resulting in the shortening of the input array to only four elements.

We have tried to provide you the best possible description on the use of array_pad() and array_pop() functions in php. Hope you liked this article. For further updates and related information, stay connected to our php blogs.

LEAVE A COMMENT

Facebook
Twitter
LinkedIn
Pinterest