Tuesday, November 19, 2024
Google search engine
HomeLanguagesPHP | array_product() Function

PHP | array_product() Function

The array_product() is an inbuilt function in PHP and it returns the product of all numbers present in the given array. The function accepts an array which consists of numbers only. If the array has any other data except numbers, the function returns 0.

Syntax:

array_product($array)

Parameters: The function has one mandatory parameter $array, for which we want to calculate the product of all values.

Return Value: This function returns three different values based on the below cases:

  • It returns 0 if the array consists of atleast one non-number data.
  • It returns 1 when an empty array is passed as a parameter.
  • If both of the above two cases are not met then it returns the product of all the terms in array.

Examples:

Input : $array = [1, 2, 3, 4]
Output : 24 

Input : $array = [1, 'a'] 
Output : 0 

Below programs illustrate the array_product() function:

Program 1: Program to demonstrate the array_product() function.




<?php
// PHP program to demonstrate 
// the array_product() function
$a1=array(1, 2, 3, 4);
  
echo(array_product($a1));
?>


Output:

24

Program 2: Program to demonstrate the array_product() function when the array contains at least one non-number data.




<?php
// PHP program to demonstrate the array_product() 
// function when the array contains at least
// one non-number data
  
$a1=array(1, 2, 3, 'a');
  
echo(array_product($a1));
?>


Output:

0

Program 3: Program to demonstrate the array_product() function when the array is empty.




<?php
// PHP program to demonstrate the array_product() function
// when the array is empty
  
$a1=array();
  
echo(array_product($a1));
?>


Output:

1

Reference:
http://php.net/manual/en/function.array-product.php

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments