how to split numbers in php?i need a PHP code for this input.if the input is 143,i need output as 8(tat is sum of 143{1+4+3})

2 Years
Discussion Span
Last Post by Gideon_1

i need a PHP code for this input

You can get help here, nobody will write it for you. So what have you tried?


Sorry..i'am a beginer..i dnt no the basic logic also for this program..i dont no how to split the numbers.please help me


I agree perfectly with pritaeas. I will advice you to go and learn the basis of php and programming. But since you are a beginner, I thought of refreshing myself with your problem. I have commented it very well for you to understand. Try it and I hope it solved your problem.

    //this is the set of numbers
    $numbers = 1432;

    //getting the number of characters(length) of the set of numbers
    $num_length = strlen($numbers);

    //Will be using substr so setting the offset to zero
    $i = 0;

    //intialising the sum variable.
    $sum = 0;

    //executing the loop
    while ($i < $num_length) {
        //adding up numbers till the last one
        $sum += substr($numbers, $i, 1);

        //incrementing $i(offset) each time the a number is added

    //outputting the total results.
    echo $sum;

So much for "nobody here will write it for you". That bit of code just undermined our ethos of helping members to help themselves. It just attracts leeches.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.