PHP method to calculate the sum and product of values ​​in an array (array_sum and array_product functions), sumproduct_PHP tutorial

WBOY
Release: 2016-07-12 08:55:22
Original
1225 people have browsed it

PHP’s method of calculating the sum and product of values ​​in an array (array_sum and array_product functions), sumproduct

The example in this article describes how PHP calculates the sum and product of values ​​in an array. Share it with everyone for your reference, the details are as follows:

1. Overview:

array_sum() function is used to calculate the sum of all values ​​in an array.
The array_product() function is used to calculate the product of all values ​​in an array.

2. Usage examples:

array_sum()

PHP array_sum() function is used to calculate the sum of all values ​​in the array, and returns the calculation result as an integer or floating point number. Non-numeric units will be treated as 0.

Syntax:

number array_sum( array array )

Copy after login

Example:

<&#63;php
$arr_a = array(1, 2, 3, "a");
echo array_sum($arr_a);  //输出:6
&#63;>

Copy after login

array_product()

PHP array_product() function is used to calculate the product of all values ​​in the array, and returns the calculation result as an integer or floating point number. Non-numeric units will be treated as 0.

Syntax:

number array_product( array array )

Copy after login

Example:

<&#63;php
$arr_a = array(1, 2, 3);
echo array_product($arr_a);  //输出:6
$arr_b = array(1, 2, 3, "abc");
echo array_product($arr_b);  //输出:0
&#63;>

Copy after login

Readers who are interested in more PHP-related content can check out the special topics on this site: "Summary of PHP operations and operator usage", "Summary of PHP network programming skills", "Introduction to PHP basic syntax", "php operation office documentation" Summary of skills (including word, excel, access, ppt)", "Summary of php date and time usage", "Introduction to php object-oriented programming tutorial", "Summary of php string (string) usage", "Introduction to php mysql database operation" Tutorial" and "Summary of Common PHP Database Operation Skills"

I hope this article will be helpful to everyone in PHP programming.

Articles you may be interested in:

  • PHP array function array_multisort() usage example analysis
  • A large summary of PHP mathematical operation functions (classics worthy of collection)
  • Detailed explanation of PHP anonymous functions and precautions
  • A summary of the usage of common array functions in PHP
  • Usage examples of PHP anonymous functions and use clauses
  • A brief talk about the strlen function in PHP
  • Analysis of the source code of array_keys and array_unique functions in PHP
  • How PHP queries and deletes duplicate data in multiple columns of the database (implemented using array functions)
  • php’s powerful time conversion function strtotime
  • PHP function timeout processing method
  • A preliminary study on PHP’s closure (Closure) anonymous function
  • Usage analysis of PHP function import_request_variables()

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1117045.htmlTechArticlePHP’s method of calculating the sum and product of values ​​in an array (array_sum and array_product functions), sumproduct This article describes PHP with examples Methods for calculating the sum and product of values ​​in an array. Share it with everyone...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template