php split number and plus them [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
With PHP I need to add all the digits in a number.
Example : if I have 142 it will be 1+4+2 = 7
its should be 1 to 9 if not its should plus again 11 will be 1+1 = 2

Here is a function, based on Dave Chen's answer :
function sumDigits($input) {
$sum = array_sum(str_split($input));
if($sum > 9) {
$sum = sumDigits($sum);
}
return $sum;
}
This code :
echo sumDigits("1239");
Outputs: 6
1 + 2 + 3 + 9 = 15 = 1 + 5 = 6

Related

How to get the position of same elements in php? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need your help to get the position of duplicate or same elements in an array.
For example
$arr =[6,5,3,7,40,45,7,6,3,32,86,40,5,3,7,40];
Result
6 = 0,7
5 = 1,11
3 = 2,8
7 = 3,6,14
40 = 4,11,15
45 =5 and so on.
One simple approach:
<?php
$arr =[6,5,3,7,40,45,7,6,3,32,86,40,5,3,7,40];
$pos = array();
foreach($arr as $k => $v) {
$pos[$v][] = $k;
}
foreach($pos as $k => $v) {
echo $k."=".implode(',', $v)."<br>";
}
?>
Result:
6=0,7
5=1,12
3=2,8,13
7=3,6,14
40=4,11,15
45=5
32=9
86=10

How to make image for each x numbers [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
How to replace 200 numbers with an image?
This example :
For example
When user have 200 numbers He takes 1 image
When user have 830 He take 4 image
what php code I need it?
Sorry but I havn't Any code
Thanks in advance
$votes = 10334;
$starCount = intval($votes/ 200);
$starCount = $starCount > 5 ? 5 : $starCount; //if maximum 5 stars
$a = 1;
$starsString = '';
for ($a; $a <= $starCount; $a++) {
$starsString .= '⛤'; // or '<img src="https://i.stack.imgur.com/EhAy4.gif" alt="here">'
}
echo $starsString;

php extract numbers and variables in math function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a math function, for example:
string = "3x+6.5y-23z"
I need to extract the function and get x, y, z; give value x = 6, y = 7, z = 8; and solve.
you can do it like this
<?php
$x = 6;
$y = 7;
$z = 8;
echo $string = 3*$x+6.5*$y-23*$z;
?>

How to display total number from start to a range in php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How to display total number from start to end in a range in php .Each time number will increase by 10 .
For example total number is 35
Then number will display this way :
1 to 10
11 to 20
21 to 30
31 to 35
$num=35;
for($i=1;$i<$num;$i=$i+10)
{
$j=$i+9;
if($j>$num)
$j=$num;
echo $i.' to '. $j;
}
try
$num=35;
for($i=1;$i<=$num;$i++) {
if($i %10 == 0)
echo $i.'<br>';
else
echo $i;
}
will output :-
12345678910
11121314151617181920
21222324252627282930
3132333435
Use a for loop like this
for($x = 1; $x <= 35, $x += 10){
echo $x . " to " . $x+9;
}
This will work i hope.
Thanks

Divide integer number into 3 parts using php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I need to divide an integer value into 3 parts using php
for eg: I have a value 9 the answer is like 3 + 3 + 3
if i have value 10 the ans like 3 + 3 + 4 or something like that
You can use a modulus function.
<?php
$yourInt=10;
$remainder=$yourInt % 3;
$third=floor($yourInt/3);
$lastBit=$third+$remainder;
echo "The numbers are $third + $third + $lastBit.";
?>
Output:
The numbers are 3 + 3 + 4.
If you have a value that is >= 3 you can do the following:
$number = 10;
$number1and2 = floor($number/3);
$number3 = $number - (2*$number1and2);
$num = 11;
$d = [$num/3,$num/3,$num/3];
$round = array_map('round', $d);
list($first, $second, $third) = $round;
$round = (is_float($num/3)) ? $num-(round($num/3)*3) : 0;
echo $first.' '.$second.' '.($third += (is_float($num/3)) ? $num-(round($num/3)*3) : 0);

Categories