Breaking News
Home / Web / PHP / PHP FAQ / Array Push/Pop Functions in PHP

Array Push/Pop Functions in PHP

Array functions like array_push and array_pop can be used to add or elements to the end of array respectively.

  1. <?php
  2. $stack = array("orange", "banana");
  3. array_push($stack, "apple", "raspberry");
  4. print_r($stack);
  5. ?>
  6. //The above example will output:
  7.  
  8. Array
  9. (
  10.     [0] => orange
  11.     [1] => banana
  12.     [2] => apple
  13.     [3] => raspberry
  14. )
  15.  
  16. <?php
  17. $stack = array("orange", "banana", "apple", "raspberry");
  18. $fruit = array_pop($stack);
  19. print_r($stack);
  20. ?>
  21. // After this, $stack will have only 3 elements:
  22.  
  23. Array
  24. (
  25.     [0] => orange
  26.     [1] => banana
  27.     [2] => apple
  28. )
[pb_builder]

Check Also

What is the structure of a PHP Program?

Program Organization is key to readability, ease of debugging, few bugs, simplicity and more. As …

Leave a Reply