tyra Posted May 1, 2009 Share Posted May 1, 2009 What am i dong wrong here and is there any other solutions to solve this problem ? <?php $num = 256; function atotal($num){ $num = strval($num); while(strlen($num) < 2){ for($i = 0; $i < strlen($num); $i++){ $total = $num{$i}; } $num = $total; if(strlen(strval($total)) != 1){ $total = 0; } } return $total; } echo atotal($num); ?> Link to comment https://forums.phpfreaks.com/topic/156388-code-not-working/ Share on other sites More sharing options...
ignace Posted May 1, 2009 Share Posted May 1, 2009 <?php function strsum($str) { if (!is_numeric($str)) return 0; $str = strval($str); for ($i = 0; $i < strlen($str); $i++) { $sum += intval($str[$i]); } return $sum; } print strsum(256); // 13 ?> Link to comment https://forums.phpfreaks.com/topic/156388-code-not-working/#findComment-823350 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.