Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to calculate the product and summation of php array

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to calculate the product and summation of php arrays". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to calculate the product and sum of php arrays.

Php array to calculate the product and summation method: 1, with array_product (), you can calculate the product of all elements in the array, syntax "array_product ($arr)"; 2, with array_sum (), you can calculate the sum of all elements of the array, syntax "array_sum ($arr)".

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

The method of calculating Product and Sum of php Array

PHP has two built-in functions that can calculate the product and summation of arrays. Let's take a look.

1. Use the array_product () function to calculate the array product

The array_product () function is used to calculate the product of all elements in an array.

Array_product ($arr)

Returns an integer value if all elements of the array arr are integers, or floating-point numbers if one or more of the values are floating-point numbers.

If there are non-numeric elements in the array arr, PHP attempts to convert them to a numeric value, which fails as a value of 0. For example, the string "29.5" is converted to the decimal 29.5, and the string "100abc" is converted to the integer 100.

Example:

2. Use array_sum () to realize array summation.

The array_sum () function is used to calculate the sum of all elements in the array, and its syntax is as follows:

Array_sum ($arr)

Returns an integer value if all elements of the array arr are integers, or floating-point numbers if one or more of the values are floating-point numbers.

If there are non-numeric elements in the array arr, PHP attempts to convert them to a numeric value, which fails as a value of 0. For example, the string "45" is converted to the integer 45 and the string "12.4abc" is converted to the decimal 12.4.

Example:

At this point, I believe you have a deeper understanding of "how to calculate the product and sum of php arrays". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report