Cep Posted February 28, 2008 Share Posted February 28, 2008 Hello, Is there an easy way to slice a number down into its separate number values? For example, <?php $num = 54321; //Magic code here taking $num and returning either, $n1 = 5; $n2 = 4; $n3 = 3; $n4 = 2; $n5 = 1; //or $narray = array(5,4,3,2,1); ?> Link to comment https://forums.phpfreaks.com/topic/93533-slicing-up-a-number/ Share on other sites More sharing options...
uniflare Posted February 28, 2008 Share Posted February 28, 2008 you would use str_split(); http://uk3.php.net/str_split eg: <?php $number = 123456789; print_r(str_split($number,1)); ?> hope this helps, Link to comment https://forums.phpfreaks.com/topic/93533-slicing-up-a-number/#findComment-479243 Share on other sites More sharing options...
pocobueno1388 Posted February 28, 2008 Share Posted February 28, 2008 <?php $num = 54321; $narray = str_split($num); echo '<pre>',print_r($narray),'</pre>'; ?> Link to comment https://forums.phpfreaks.com/topic/93533-slicing-up-a-number/#findComment-479245 Share on other sites More sharing options...
uniflare Posted February 28, 2008 Share Posted February 28, 2008 echo '<pre>',print_r($narray),'</pre>'; would this work? shouldnt it be: echo '<pre>'.print_r($narray).'</pre>'; ? Link to comment https://forums.phpfreaks.com/topic/93533-slicing-up-a-number/#findComment-479251 Share on other sites More sharing options...
Cep Posted February 28, 2008 Author Share Posted February 28, 2008 Thanks peeps! Link to comment https://forums.phpfreaks.com/topic/93533-slicing-up-a-number/#findComment-479261 Share on other sites More sharing options...
pocobueno1388 Posted February 28, 2008 Share Posted February 28, 2008 echo '<pre>',print_r($narray),'</pre>'; would this work? shouldnt it be: echo '<pre>'.print_r($narray).'</pre>'; ? Yes, you can do concatenation with commas or periods. I only use the comma when I'm using print_r() as I did above. For some reason a period won't work with that, I'm honestly not exactly sure why. Must be something to do with using a function in an echo statement or something. Link to comment https://forums.phpfreaks.com/topic/93533-slicing-up-a-number/#findComment-479263 Share on other sites More sharing options...
uniflare Posted February 28, 2008 Share Posted February 28, 2008 i never kew uu could concatenate with commas, thanks, stange problem also. glad to see the bloke is happy Link to comment https://forums.phpfreaks.com/topic/93533-slicing-up-a-number/#findComment-479266 Share on other sites More sharing options...
aschk Posted February 28, 2008 Share Posted February 28, 2008 Bear in mind there is a difference between concatenation with dots (.) and commas (,) I'll leave you to figure out what it is Link to comment https://forums.phpfreaks.com/topic/93533-slicing-up-a-number/#findComment-479278 Share on other sites More sharing options...
PFMaBiSmAd Posted February 28, 2008 Share Posted February 28, 2008 Reading the definition of echo in the manual will tell you why the , works. The , does not mean to concatenate. Link to comment https://forums.phpfreaks.com/topic/93533-slicing-up-a-number/#findComment-479296 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.