What is the difference between array PUSH and POP? explain with example

In PHP, the array methods like array_push and array_pop are used to add or remove elements or items from the array. The array_push method can use to add or insert one or more items or elements from the end of an array. and The array_pop() method can use to extract, delete and remove the elements/items from the end of the array.

array_push() function

PHP array_push() is a built-in function used to insert new elements at the end of an array and get the updated array elements. The array_push() method takes a single element or an array of elements and appends it to the array.

Syntax:-

array_push(array,value1,value2...)

Parameters

An array parameter is required to which we will add the value.

The value1 parameter is also required, which is the value we will insert into the specified array.

The value2, value3, and so on are optional parameters. However, we need to pass those parameters if we want to add multiple values.

Example:-

<?php
    $nicname = ["Vijay", "Amit", "Dharmu"];

    array_push($nicname, "Ravi", "Rahul");

    echo "<pre>";
    print_r($nicname);
    echo "</pre>";
?>

Output:-

array_pop() function

PHP array_pop() function to remove an element or value from the end of an array. The array_pop() function also returns the last value of an array. However, if the array is empty (or the variable is not an array), the returned value will be NULL.

Syntax:-

array_pop(array);

Example:-

<?php
    $nicname = ["Vijay", "Amit", "Dharmu", "rahul"];

    array_pop($nicname);

    echo "<pre>";
    print_r($nicname);
    echo "</pre>";
?>

Output:-

Related Posts

PHP – strip_tags() Replace with Spaces Example

We are going to learn php strip tags with spaces. You will learn how to replace php Strip_tags with space. In this article, we will replace php…

How to Get Duplicate Values from Array in PHP?

We will learn php array get duplicate values. we will help you to give an example of how to get duplicate values from array in php. you…

PHP explode() Function Example Tutorial

A PHP explode function example is given in this article. I will demonstrate how to use PHP’s explode string to array function. This is a basic example…

How to Convert Array to String in PHP?

We will cover the example of converting an array to a string in PHP in this lesson. We’ll examine an example of converting an array to a…

How Can I Convert a String in PHP Into an Array?

We will go over how to transform a string into an array in PHP in this little tutorial. Describe how to convert a string value into an…

How to Use the array_merge() Function in PHP?

We will go over the simple and quick method to use the PHP array_merge() function example in this brief tutorial. Let’s talk about the PHP example of…

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x