slyte33 Posted January 6, 2013 Share Posted January 6, 2013 Hello everyone! I cant seem to figure this out... here's a code for example: $number = "20"; echo "2"; echo "0"; If you're wondering why I need something like this it's because I'm making the number show in a php image, but instead of outputting numbers 0-99, I could split the number 20 into "2" and "0" to output only numbers 0-9 Any help would be greatly appreciated Link to comment https://forums.phpfreaks.com/topic/272744-breaking-down-the-number-20-into-echo-2-echo-0/ Share on other sites More sharing options...
us2rn4m2 Posted January 6, 2013 Share Posted January 6, 2013 Hello, $number = "20"; str_split($number); echo $number[0]; echo '<br />'; echo $number[1]; /* Result 2 0 */ Link to comment https://forums.phpfreaks.com/topic/272744-breaking-down-the-number-20-into-echo-2-echo-0/#findComment-1403520 Share on other sites More sharing options...
requinix Posted January 6, 2013 Share Posted January 6, 2013 If it's always (at least) two digits you can simply $number = "20"; echo $number[0]; echo $number[1]; Link to comment https://forums.phpfreaks.com/topic/272744-breaking-down-the-number-20-into-echo-2-echo-0/#findComment-1403522 Share on other sites More sharing options...
slyte33 Posted January 6, 2013 Author Share Posted January 6, 2013 Hello, $number = "20"; str_split($number); echo $number[0]; echo '<br />'; echo $number[1]; /* Result 2 0 */ If it's always (at least) two digits you can simply $number = "20"; echo $number[0]; echo $number[1]; Awesome, thanks both of you Link to comment https://forums.phpfreaks.com/topic/272744-breaking-down-the-number-20-into-echo-2-echo-0/#findComment-1403523 Share on other sites More sharing options...
us2rn4m2 Posted January 6, 2013 Share Posted January 6, 2013 // correction $number = "20"; $n = str_split($number); echo $n[0]; echo $n[1]; Link to comment https://forums.phpfreaks.com/topic/272744-breaking-down-the-number-20-into-echo-2-echo-0/#findComment-1403524 Share on other sites More sharing options...
Christian F. Posted January 7, 2013 Share Posted January 7, 2013 Example of the method shown by requinix, for a variable length number: $string = 3251; $max = strlen ($string); for ($run = 0; $run < $max; $run++) { echo $string[$run]."\n"; } Link to comment https://forums.phpfreaks.com/topic/272744-breaking-down-the-number-20-into-echo-2-echo-0/#findComment-1403907 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.