Jump to content

Recommended Posts

PHP newbie!

 

I've looked through the forums, but couldn't find anything that helps me with my problem.  I'm trying to write the code for a calculation that adds the individual digits in a number, and if the answer comprises more than 1 digit, repeats the formula.  I've successfully done this in excel and vb using the MID function, but since I'm new to php I have no idea how to proceed.  I tend to gather substr() would be useful here.

 

The logic of the calculation is as follows:

 

(1) Determine if the length of the number exceeds 1 ... else just use the number

(2) Loop from the first digit to the last digit (as determined by the length) adding each digit

(3) Assign the calculated number as the new number and start again at step 1.  This iteration repeats until a single digit remains which becomes the final number for use.

 

It would be unfair for me to expect anyone to write the code for me (although I wouldn't mind  ;) ), but a few hints on how to proceed with the code would be great.

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/127869-calculate-digits-in-number/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.