I want to get the single digits from two digits.for example if 36 I need to store 3 and 6 in different variables.Is it possible?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You solve this with simple Mathematics.
First of all, think about what 36 really is, if you break it down, its
3 * 10 + 6, right?So the first digit in this case represents your times 10, which you will get by dividing by 10:
Now if you round that you get 3, which is the first digit.
How about the rest of that? Well, for that you have to use something called modulo, which can be hard to understand some times. But it baslicy takes out the left overs of a integer division.
IT bascily means that when you do
36 % 10you get 6. Why is that you might think? Try open a calculator and push in the numbers:36 / 10 = 3.6, the left overs are 6!Solution Code
You can look into the function floor here.
Alternative Solution for splitting strings
If you are looking for alternative ways to split strings in PHP you can use str_split, this will provide you with an array of characters.
Example
Just use that one as an array