$variable = 123 ;
? best method to retrieve the sum of the $variable as Eg - 1+2+3 = 6
Printable View
$variable = 123 ;
? best method to retrieve the sum of the $variable as Eg - 1+2+3 = 6
I don't know if this is the best way. It is one way:
PHP Code:
<?php
$variable = 123;
$va = str_split($variable);
$result = 0;
foreach ($va as $v){
$result += $v;
}
echo $result;
?>
That would work (and for some reason it's appealing to me because I like foreach).
The best way would be to start with another kind of input (like an array).
The most efficient way* is as follows:
This uses a trick where it treats this number as a string (technically, to not rely on a trick, you could convert it first), then it treats that string as an array of characters.PHP Code:
$n = 123;
$o=0;
for($i=0;$i+1<strlen($n);$i++) {
$o+=$n[$i];
}
echo $o; //5
(*I'm talking about the shortest code to do this. For the most efficient machine processing, it's probably with while, imitating the for-loop, as you taught me a long time ago, John. You could obviously convert this one if you want.)
The trick doesn't work here. I get 0, and the correct result is 6 not 5, so you probably didn't try it out. Or if you did and got 5, it still doesn't work. This works:
What you're calling a trick is known as type conversion. Various languages will or will not do it. Of those that will some will do it in one sort of circumstance and not another, sometimes without any immediately obvious reason. Both of my snippets rely upon type conversion as well. First that a number will be treated as a string and be split into an array, and second that the now string representations of the individual numbers will be treated as numbers when added to $result. In both cases this appears to go along fine. In your code I'm not sure where it doesn't convert as expected, or even if the lack of an expected type conversion is the problem. But, I get no errors and the result here for that code is 0.PHP Code:
<?php
$variable = 123;
$va = str_split($variable);
$result = 0;
$c = count($va);
while (--$c > -1){
$result += $va[$c];
}
echo $result;
?>
Ah, if I type convert your $n:
Then it does work and gives 5, which is wrong because here:Code:$n = 123
. ''
;
1 is added, which skips the first character/digit.Code:for($i=0;$i
+1
<strlen($n);$i++) {
So I guess your's might 'work' in some version of PHP if it type converts within the loop. Mine (PHP 5.3) doesn't. It gives a null or falsey value to each $n[$i] in the loop.
It appears that, in my version of PHP at least, type conversion occurs number to string when an explicit string function is used upon a number, but not when a string convention ($str[$index]) is. String to number occurs in all of these, when they work, when using an arithmetic operator upon a string that can be a number.
Sorry, I didn't test it. (And my math was off-- just quick in-my-head calculation... haha, I can't add.)
Right, if you type convert it with the empty string, it works.
I didn't realize that. I guess it's just slightly too much to use an integer as a string as an array. (Each conversion works independently though!)
As for the loop, just play with it a bit-- subtract one, add one, whatever works best. (Indices start at 0, strlen() is the number of characters, and we want it to go to one less than that-- I don't think the middle argument is the problem.)
If you can't figure it out I can check later. I'm just busy at the moment. This does work, in theory.
As I said in my last post, I prefer the foreach method because I know exactly what is going on there intuitively and wouldn't end up with typos like this :) (But technically this method is more efficient.)
PHP Code:
<?php
$i = 123;
$sum_of_digits = array_sum( str_split( $i ) );
print $sum_of_digits; // 6
Haha, that works. I wasn't even thinking about the sum, but about separating the digits. That's probably simplest. (Although at the most technical level, I don't know how efficient those functions are if speed is really the concern. I'm sure it would be fine. Addition isn't, in any way, very taxing on the system.)
In javascript we are taught that, if there's an internal method of doing something (native function), it's almost always more efficient than doing it yourself using more basic functions. So I would vote for Adrian's method as probably best. Time trials would need to be used to know for sure.
PHP Code:
$variable = 123;
echo array_sum(str_split($variable));
Which also holds true in PHP.
just for kicks:
Averages of multiple 1,000-iteration trials:
PHP Code:
# my method
$variable = 123;
echo array_sum(str_split($variable));
# 0.0032110214233398 sec
PHP Code:
# John's second method
$variable = 123;
$va = str_split($variable);
$result = 0;
$c = count($va);
while (--$c > -1){
$result += $va[$c];
}
# 0.0034551620483398 sec
...but, as you see, not much difference in this particular case. Two milliseconds between the fastest and slowest methods isn't very much. And remember, that's for 1,000 iterations.PHP Code:
# John's first method
$variable = 123;
$va = str_split($variable);
$result = 0;
foreach ($va as $v){
$result += $v;
}
# 0.0032589435577393 sec
Isn't that 2 tenths of a millisecond?
Oddly enough, foreach is faster than while (or for) loops. I'm surprised. It's a sort of weird feature in PHP, but I love it. Now I like it more.
In javascript we are taught that, if there's an internal method of doing something (native function), it's almost always more efficient than doing it yourself using more basic functions.[/quote]
Which also holds true in PHP.[/quote]On average, and perhaps especially when PHP sometimes has default functions that are based in C++ (I think, and even if so I'm not sure which ones). But I'd imagine that sometimes it's overkill to use something-- for example, I imagined that foreach was a more complicated kind of loop. Here it turns out that particular example was wrong, of course. It was efficient.
By the way, letom, have we answered your question? You now have a few versions to pick from so you can use whatever you want. If you need more help, feel free to ask :)