What is diff. b/w array PUSH and POP? Explain with example?

Posted by

The array_push() and array_pop() methods in PHP is used to perform insertions as well as deletions from the object. array_push is used to add value in the array(it always add new value at the last place) and array_pop is used to remove last value from the array.

syntax

array_push($array, $elements)   //array push
array_pop($array)      //array pop

example

<?php

		$color=array("red","green");
		
		array_push($color,"blue","yellow");
		
		  echo "<pre>";
		  
		print_r($color);
		
		 echo "</pre>";
		 
		 
		 echo "<br>";
		 echo "<br>";
		 
		
		$color=array("red","green","yellow","blue");
		
		array_pop($color);
		
		  echo "<pre>";
		  
		print_r($color);
		
		 echo "</pre>"; 
?>

output

Array
(
    [0] => red
    [1] => green
    [2] => blue
    [3] => yellow
)


Array
(
    [0] => red
    [1] => green
    [2] => yellow
)
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x