Home > Backend Development > PHP Problem > How to write a function in php that inputs an array and sums it

How to write a function in php that inputs an array and sums it

PHPz
Release: 2023-04-23 10:01:33
Original
785 people have browsed it

In PHP development, arrays are often one of the main data structures we use to manipulate data. However, when dealing with large amounts of array data, we often need to perform numerical operations on it, such as summation. This article will teach you how to write a PHP function that sums arrays.

  1. Understand the meaning and requirements

Before using the PHP sum function, we need to determine the specific requirements and usage scenarios of the function. In this article, our array sum function needs to meet the following requirements: The

  • function can receive an array as a parameter; the
  • function can perform a sum operation on all elements in the array ;
  • The function can return the summation result;
  • The function needs to ensure that the input array is all numerical data.

Based on the above requirements, we can first write simple pseudo code:

function array_sum($arr) {
  // 数组求和逻辑
  return $sum;
}
Copy after login
  1. Implement array summation logic

Next, We need to think about how to implement array summation logic. The conventional method is to use a loop to add each element in the array. The code is as follows:

function array_sum($arr) {
  $sum = 0;
  foreach ($arr as $num) {
    $sum += $num;
  }
  return $sum;
}
Copy after login

Code explanation:

  • First, we define a variable$ sum to save the accumulated result;
  • Then, we use foreach to loop through each element in the array$num;
  • In Within the loop body, we add $num to $sum;
  • Finally, we return the summation result $sum.
  1. Add input validation

However, the above code only applies when all elements in the input array are numeric. If the input array contains non-numeric data, the code will throw an error and the function will not work properly. For more robust and robust code, we need to add input validation.

The specific implementation is to add input verification logic to the loop body and throw an exception when an input error occurs:

function array_sum($arr) {
  $sum = 0;
  foreach ($arr as $num) {
    if (!is_numeric($num)) {
      throw new InvalidArgumentException('Input array must contain numeric elements only');
    }
    $sum += $num;
  }
  return $sum;
}
Copy after login

Code explanation:

  • We use is_numeric() Function to determine whether the array element $num is a numeric type;
  • If $num is not a numeric type, we throw a InvalidArgumentException Exception and prompt error message;
  • If $num is a numeric type, we add it to $sum;
  • Finally, we return the summation result $sum.
  1. Add return type declaration

Finally, we can further add return type declaration to the function code to facilitate better reading and code review:

function array_sum(array $arr) : float {
  $sum = 0;
  foreach ($arr as $num) {
    if (!is_numeric($num)) {
      throw new InvalidArgumentException('Input array must contain numeric elements only');
    }
    $sum += $num;
  }
  return $sum;
}
Copy after login

Code explanation:

  • First, we add a return type declaration to the function to indicate the type of function return value;
  • The return type declaration includes two parts: The data type after the colon float, and the declaration array before the function parameter list;
  • In the code, we use array to declare the function The parameter $arr must be of array type;
  • Every time a numeric element is successfully scanned, we will accumulate it into $sum;
  • Finally, we return the summation result $sum.
  1. Summary

It can be seen that writing a PHP function that performs array sum is relatively simple syntactically. We only need to understand the problem, think about the logic, and add input verification and return type declaration based on the needs and scenarios, and we can quickly implement a robust and efficient PHP array sum function.

The above is the detailed content of How to write a function in php that inputs an array and sums it. For more information, please follow other related articles on the PHP Chinese website!

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