w3resource

PHP: array_push() function

PHP: Push one or more elements onto the end of array

The array_push() function is used to add one or more elements onto the end of an array. The length of array increases by the number of variables pushed.

Version:

(PHP 4 and above)

Syntax:

array_push(array_name, value1, value2...)

Parameters:

Name Description Required /
Optional
Type
array_name The input array Required Array
value1 Value to add. Required Mixed*
value2.. Value to add. Optional Mixed*

*Mixed: Mixed indicates that a parameter may accept multiple (but not necessarily all) types.

Return value:

The new number of elements inserted in array_name.

Value Type: Integer.

Example:

<?php
$array1= array('Mathematics','Physics');
array_push($array1,'Chemistry','Biology');
print_r($array1);
?>

Output:

Array ([0] => Mathematics [1] => Physics [2] => Chemistry [3] => Biology)

Pictorial Presentation:

php function reference: array_push() function

View the example in the browser

Practice here online :

See also

PHP Function Reference

Previous: array_pop
Next: array_rand



PHP: Tips of the Day

PHP: How to convert string to boolean?

Strings always evaluate to boolean true unless they have a value that's considered "empty" by PHP (taken from the documentation for empty):

  1. "" (an empty string);
  2. "0" (0 as a string)

If you need to set a boolean based on the text value of a string, then you'll need to check for the presence or otherwise of that value.

$test_mode_mail = $string === 'true'? true: false;

EDIT: the above code is intended for clarity of understanding. In actual use the following code may be more appropriate:

$test_mode_mail = ($string === 'true');

or maybe use of the filter_var function may cover more boolean values:

filter_var($string, FILTER_VALIDATE_BOOLEAN);

filter_var covers a whole range of values, including the truthy values "true", "1", "yes" and "on".

Ref : https://bit.ly/33SQ3eP