Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Stu Cowley
26,287 PointsAdding Spaces to Numerals in PHP
Hey guys,
So I've finally got the confidence in PHP to actually start using it, but I need to know one small detail about using numbers, especially with phone numbers.
I have a list of phone numbers for an array of cafe's in my local area. I have added all of them to an associative array and it's running exactly how I want it seems a bit silly to use a string for phone numbers.
So you get a basic idea of what I'm looking to do I've included a code snippet
array('Cafe Lime', '3 Railway Terrace, Goolwa', '8555 2011')
When I just declare the phone number as a set of numbers instead of a string I can't simply add the space to the number to format it nicely or else I get odd results.
I hope I've made some sense.
Thanking you in advance
Stu :)
3 Answers

Jason Anello
Courses Plus Student 94,610 PointsHi Stu,
I wouldn't say it's silly to use a string for phone numbers. You're not going to be doing mathematical operations on phone numbers so they don't really need to be numbers.
I don't know if you will have leading zero's but that will be a problem if storing as numbers.

Ron McCranie
7,837 Pointsyou could make a helper function for formatting the number from the integer format you're storing it as to a sting when you display it. Assuming the phone numbers follow a simple pattern like "#### ####" you could make one phone() function to format it.
function phone($number) {
return substr($number, 0,4).' '.substr($number, 4);
}
Then your code above, when showing the data could be
array('Cafe Lime', '3 Railway Terrace, Goolwa', phone(85552011));

Stu Cowley
26,287 PointsThanks guy,
Jason Anello that makes really good sense! Thanks so much. As I'm new to the language I just want to be sure I'm doing things correctly.
Ron McCranie thanks also for you input, I have added your advice to my PHP Bible notebook for future reference as I feel that this would be extremely handy down the track!
Thanks again,
Stu : )