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

Associative Array in PHP?

An associative array is a simple and very important array of PHP. This array is also used the most, it is very easy to understand this array,…

How to work in functions PHP?

If there is any task in your program which you train to execute repeatedly then instead of writing the code for that task at a different place…

For Loofs in PHP

In for-loop initialization, condition and increment happen on the same line. The loop keeps on repeating as long as the condition is true. When the for loop…

If Else Conditionals in Php?

If Else statement is the most important feature of any programming language, PHP is used to run any Code Of Block according to If Else conditions. This…

What are operators in PHP? PHP – Operator Types

Operators are used to developing data. In Programming Languages, Operators are taken from Mathematics and Operator Programmers work with Data. An operator is a chain of symbols…

String Functions in PHP?

When many characters are written together in sequence, they form a string. For example, India is a string. It is made up of i, n, d, i,…

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