Summing numbers in wordpress loop [duplicate] - php

This question already has answers here:
Sum values in foreach loop php [closed]
(5 answers)
Closed 8 years ago.
I have no idea how can I display a sum of numbers from array. I work inside wordpress loop. Here is my code:
<?php
$a = array($apples);
foreach ($a as $b)
{
$totalapples = $b . " ";
echo "$totalapples";
}
?>
This code display (I have numbers like 4,5 etc.):
4,5 13,5 10 13,5 14 12,3 7,5 5,4 9,5 5,4 4,5 4 3,3 5,7

To add numbers, use +=.
$totalapples = 0;
foreach ($apples as $apple) {
$totalapples += $apple;
}

Related

Performing arithmetic operations on arrays [duplicate]

This question already has answers here:
PHP FizzBuzz Logic
(3 answers)
Another FizzBuzz solution [closed]
(3 answers)
PHP Fizzbuzz Challenge [closed]
(3 answers)
PHP loop increment quirk - (FizzBuzz in one line)
(4 answers)
Closed 2 months ago.
I came across this php interview question while researching pre-interview test questions.
Given an array of integers, compute a total score based on the following rules:
Add 1 point for every even number in the array
Add 3 points for every odd number in the array
Add 5 points for every time you encounter an 8 in the array
Examples:
Input: my_numbers = [1, 2, 3, 4, 5]
Output: 11
Input: my_numbers=[15, 25, 35]
Output: 9
Input: my_numbers=[8, 8]
Output: 10
How to approach this particular task?
my attempt
<?php function find_total( $my_numbers ) {
//Insert your code here \
$total = 0;
foreach($my_numbers as $val) {
if ($val % 2 == 0) {
echo (++$val);
} else
echo ($val += 3);
}
if ($val == 8) {
echo($val += 5);}
}
?>
I am not sure your sample answers are correct (8 is an even number and should attract 1 for that as well as the 5), but to give the answer you asked for use this (PHP):
(EDITED in response to mickmackusa's and Robin Bastiaan's comments)
$total = 0;
foreach($myarray as $value) {
if ($value === 8) {
$total += 5;
} elseif (!($value % 2)) {
++$total;
} else
$total += 3;
}

Preventing for loop from exceeding condition in PHP [duplicate]

This question already has answers here:
Can you 'exit' a loop in PHP?
(6 answers)
Closed 4 years ago.
How to prevent for loop from exceeding condition?
Loop below returns 25. I want it to return 20.
for ($i = 5; $sum < 23;)
{
echo $sum += $i;
}
This loop is just an example. There will be variables with any value in place of 5 and 23
You have to write your loop differently.
A for loop is designed to run until the conditions evaluates false. In your case, the loop at sum = 20 will still run, becuase sum < 23 evaluates to true. So if you want 20, simply write $sum < 20.
Or if I give it a second thought, you may want to do it like that:
<?PHP
$sum = 0;
for($i = 5; ($sum+$i) < 23;)
{
$sum += $i;
}
echo $sum;
?>

Unable to understand the working of PHP recursive function [duplicate]

This question already has answers here:
Understanding recursion [closed]
(20 answers)
Closed 4 years ago.
I find it difficult to understand the working of this recursive function in PHP. I am not able to follow the code written at return statement. How the total of numbers from 1 to 10 is added? I eagerly want to understand this line return $count + sum($count + 1);
My whole code is:
<?php
function sum($count)
{
if($count <= 10)
{
echo $count;
echo "<br />";
return $count + sum($count + 1);
}
}
$result = sum(1);
echo "The total is $result";
?>
Output:
1
2
3
4
5
6
7
8
9
10
The total is 55
How the total 55 is received in my code? I want to learn it step by step.
Define the function
function sum($count)
{
Check if $count <=10 so the function will not excuted if
$count > 10
if($count <= 10)
{
Print $count followed by a line break
echo $count;
echo "<br />";
Return $count + the result of the same function for $count+1 this which make the function work until reach the if condition $count<=10
return $count + sum($count + 1);
}
}
Note that while you pass 1 as a parameter and the function call itself within it so it will be continues until it reaches 10
$result = sum(1);
echo "The total is $result";
?>

Random number generated and divided in to 3 numbers randomly in php [duplicate]

This question already has answers here:
How to make 5 random numbers with sum of 100 [duplicate]
(8 answers)
Closed 8 years ago.
Can a random number being generated and split/divided into let say 3 individual numbers randomly in PHP? (so they add up to the random number)
Basically I have a random number (range from 0 to 15)
For example:
There are 14 available now
- 2 in Gotham City
- 8 in Lakeview Hills
- 4 in Eskimoville
(just a dummy to give you the idea)
IsnĀ“t there a simple approach to accomplish this?
Have a nice weekend!
basic implementation would need error checking + not 100% random
<?php
//first initial number
// range must be 3-15 to get 3 random additions
$i = rand(3,15);
echo $i;
// generate what 3 number adds to $i
$one = rand(1,$i-2);
$i-=$one;
$two = rand(1,$i-1);
$i -= $two;
$three = rand(1,$i);
$i -= $three;
if($i > 0){
// add remained to third number
$three += $i;
}
echo $one;
echo $two;
echo $three;
?>

PHP foreach add code for every 4 results found [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
On every third iteration in PHP
Using PHP foreach to get a series of results,how would I add the following echo '<div class="clear"><div>' every 4 results
Any ideas?
Using modulos can do the trick :
$i = 1;
foreach($data as $result) {
if($i % 4 === 0) {
echo '<div class="clear"></div>';
}
$i++;
}

Categories