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})

Recommended Answers

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.

Jump to Post

All 7 Replies

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.

<?php
    //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
        $i++;
    }

    //outputting the total results.
    echo $sum;
?>
Member Avatar

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.

Thanks to Every one.

You are welcome, but don't forget to mark it as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.