In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "php how to find the sum of odd numbers in an array", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's make up a small series to take you to learn "php how to find the odd sum in the array"!
Methods: 1. Define a variable with "$sum=0;" to store the calculation result;2. Loop through the array with "foreach($arr as $v){}";2. In the loop body, check whether the array elements are odd, and if so, add them together, syntax "if($v%2!= 0){$sum+=$v;}"。
Operating environment of this tutorial: Windows 7 system, PHP7.1 version, DELL G3 computer
Php Find the odd sum in an array
Realization of ideas:
Define a variable assigned 0 to store the result of the calculation.
Loop through arrays using foreach statements
In the loop body, check array elements one by one for odd numbers; add them if they are odd
Check for odd numbers:
Determine whether the element is divisible by 2, and if not, odd. Grammar:
Element %2!= 0
Complete implementation code:
As you can see, in the above example, the odd elements are: 1, 3, 5, 7, 9, we use the counter to add up is not 25
At this point, I believe that everyone has a deeper understanding of "how to find the odd sum of arrays in php", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to 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: 213
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.