0

Im doing a php activity, that will compute the sum of given number of integer. Sample Output:

 int number = 367;
 Final output 
 16

16 is the total sum of 3+6+7.

Here's my Code:

$number = "367";
$int = 0;

for ($i = 0 ; $i < number.length(); $i++ ){
    $char = number.charAt(i);
    $int += Integer.parseInt(char1.toString());
}
echo $int;

Still i can't get the right answer anyone can help me? Thank you !

regards jay

2
  • How come you are using javascript in PHP? Commented Jan 14, 2013 at 3:07
  • its not javascript sir. -_- Commented Jan 14, 2013 at 3:09

1 Answer 1

1

I'm not exactly sure what you're doing but it seems like you're mixing PHP and Javascript. If you're looking for a PHP solution it's a lot easier than that. Give this a try:

$number = 367;
echo array_sum(str_split($number));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.