tags:

views:

192

answers:

6

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?

A: 

This is not PHP code!! (Didn't see the tag)

int num = 36;
int first = num / 10; // 3
int second = num % 10; // 6
vhanda
And since when did PHP-code look like that?
Filip Ekberg
Don't be so harsh- sometimes I miss the tags too!
RichardOD
Oops! Sorry I didn't see the tag. :-\
vhanda
Thanks to everyone...Now only I got the Netconnection...And now I got the answer..Thanks a lot
vinothkumar
A: 

This way:

$int = 36;
$str = (string)$int;
var_dump($str[0], $str[1]);
Petr Peller
What's wrong with my solution? I just used php potential instead of moduling :P ... and for longer numbers its definitely much easier than dividing/moduling in cycle.
Petr Peller
Thanks to everyone...Now only I got the Netconnection...And now I got the answer..Thanks a lot
vinothkumar
+1 wrong downvote compensation
stereofrog
A: 

One of the ways ..

http://uk2.php.net/manual/en/function.substr.php

// Accessing single characters in a string
// can also be achieved using "square brackets"
$string = 'abcdef';
echo $string[0];                 // a
echo $string[3];                 // d
echo $string[strlen($string)-1]; // f
Wbdvlpr
Seriously, that is really ugly for what he wants to do.
Filip Ekberg
@Filip not than you
Wbdvlpr
Thanks to everyone...Now only I got the Netconnection...And now I got the answer..Thanks a lot
vinothkumar
+5  A: 

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:

36 / 10 = 3.xxxxx

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 % 10 you 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

<?php
    $theNumber = 36;

    $first = floor($theNumber / 10);

    $second = $theNumber % 10;    
?>

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

<?php
    $myString = "36 is my number";

    $splittedString = str_split($myString);

    echo $splittedString[0];
    echo $splittedString[1];
?>

Just use that one as an array

Filip Ekberg
36 % 6 is not equal to 6. It's 0! 36 % 10 is equal to 6.
Petr Peller
Ops, my mistake. Sorry! :)
Filip Ekberg
Your 'alternative' solution is obviously the best one. There's no need for maths at all. With PHP's weak typing, you can use string manipulation functions on integers without problems. They work for n digits, instead of just 2, and are probably faster as well.
Duroth
If he ever wants to do it in another language, it's important to understand some points first. I rahter a couple of good solutions than sticking to one particular.
Filip Ekberg
Thanks to everyone...Now only I got the Netconnection...And now I got the answer..Thanks a lot
vinothkumar
+1  A: 

A string can be accessed as an array. If you convert your number to a string (or if it already is one - you don't mention), you can address the digits individually and then optionally convert them back to integers:

$num = strval(36);

echo intval($num[0]); // 3
echo intval($num[1]); // 6
drewm
Why would you convert a string into array while you can access characters as it would already be an array?
Petr Peller
Excellent point - I've updated my answer.
drewm
Thanks to everyone...Now only I got the Netconnection...And now I got the answer..Thanks a lot
vinothkumar
+2  A: 

basically

"string" way

$s = (string) $i;
$one = $s[0];
$two = $s[1];

"math" way

$two = $i % 10;
$one = ($i - $two) / 10;
stereofrog
Thanks to everyone...Now only I got the Netconnection...And now I got the answer..Thanks a lot
vinothkumar