A method of calculating the sum and product of values in an array in PHP (Array_sum and ARRAY_PRODUCT functions) _php techniques

Source: Internet
Author: User

This article illustrates the method of calculating the sum and product of the values in a PHP array. Share to everyone for your reference, specific as follows:

First, overview:

The Array_sum () function computes the and of all the values in the array.
The Array_product () function is used to compute the product of all the values in an array.

Second, the use of examples:

Array_sum ()

The PHP array_sum () function computes the and of all the values in the array, returning the result as an integer or floating-point number, and the Non-numeric cell is treated as 0.

Grammar:

Number Array_sum (array array)

Example:

<?php
$arr _a = Array (1, 2, 3, "a");
Echo array_sum ($arr _a);  Output: 6
?>

Array_product ()

The PHP array_product () function calculates the product of all the values in an array, returns the result as an integer or floating-point number, and the Non-numeric cell is treated as 0.

Grammar:

Number array_product (array array)

Example:

<?php
$arr _a = Array (1, 2, 3);
Echo array_product ($arr _a);  Output: 6
$arr _b = Array (1, 2, 3, "abc");
Echo array_product ($arr _b);  Output: 0
?>

More interested in PHP related content readers can view the site topics: "PHP Operations and Operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Basic Grammar Introductory Tutorial", "PHP operation Office Document Skills Summary (including word,excel,access, PPT), "The PHP date and time usage summary", "PHP object-oriented Programming Introduction Course", "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.