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); ?> Quote Link to comment 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, Quote Link to comment 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>'; ?> Quote Link to comment 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>'; ? Quote Link to comment Share on other sites More sharing options...
Cep Posted February 28, 2008 Author Share Posted February 28, 2008 Thanks peeps! Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.