php

PHP range(), array_sum() and array_product Functions

range() in PHP is used to create an array with numeric keys (starting from 0) in the specified range. It takes three parameters.

Syntax:
range(first,last,step)

Parameters:

  1. First takes an integer used to specify the first value in the range.
  2. Last takes an integer used to specify the last value in the range.
  3. This step is an optional parameter that skips the values present in the range.

Let’s see examples to understand this concept better.

Example 1:

Let’s create an array in a range from 0 to 10 using range().

<?php
//create an array in a range from 0 to 10
$array_input = range(0,10);
print_r ($array_input);

?>

Output:

We can see that the array is created with elements 0 to 11 associated with keys.

Example 2:

Let’s create an array in a range from 0 to 10 by skipping 4 using range().

<?php
//create an array in a range from 0 to 10 by skipping 4
$array_input = range(0,10,4);
print_r ($array_input);

?>

Output:

We can see that the array is created with elements 0 to 11 associated with keys by skipping 4 every time.

PHP array_sum()

array_sum() in PHP is used to return the sum of values present in the array. It takes array_input as a parameter.

Syntax:

array_sum(array_input)

Parameters:
array_input is the input array with numeric values.

Let’s see examples to understand this concept better.

Example 1:

Let’s create an array that has elements from 0 to 10 using the range() function and return the sum of elements.

<?php
//create an array in a range from 0 to 10
$array_input = range(0,10);

print_r($array_input);
#return sum of elements present in the above array
print("Sum of elements: ");
print_r (array_sum($array_input));

?>

Output:

The sum of the elements is 55.

Example 2:

Let’s create an array that has elements -2,3,4,5,6 using the array() function and return the sum of elements.

<?php
//create an array with elements
$array_input = array(2,3,4,5,6);

print_r($array_input);
#return sum of elements present in the above array
print("Sum of elements: ");
print_r (array_sum($array_input));

?>

Output:

The sum of the elements is 20.

PHP array_product()

array_product() in PHP is used to return the product of values present in the array. It takes array_input as a parameter.

Syntax:

array_product(array_input)

Parameters:
array_input is the input array with numeric values.

Let’s see examples to understand this concept better.

Example 1:

Let’s create an array that has elements from 1 to 10 using the range() function and return the product of those elements.

<?php
//create an array in a range from 1 to 10
$array_input = range(1,10);

print_r($array_input);
#return product of elements present in the above array
print("Product of elements: ");
print_r (array_product($array_input));

?>

Output:

The product of the elements is 3628800.

Example 2:

Let’s create an array that has elements -2,3,4,5,6 using the array() function and return the product of elements.

<?php
//create an array with elements
$array_input = array(2,3,4,5,6);

print_r($array_input);
#return product of elements present in the above array
print("Product of elements: ");
print_r (array_product($array_input));

?>

Output:

The product of the elements is 720.

Conclusion

In this PHP arrays tutorial, we saw how to create an array using the range() function by considering all parameters. Array_sum() in PHP is used to return the total sum of elements, and array_product() in PHP is used to return the product of elements present in the array.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain