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

PHP PHP Basics Unit Converter Floats

Int Vs Float

She said that "PHP handles the differences on int and float so you don't have to.", or something along those lines. Does that mean I can use an int with decimal? When do I use float and when do I use int? I understand the decimal differences, but if PHP will correct it, what is the best data type to use?

2 Answers

Robert Gouveia
Robert Gouveia
Courses Plus Student 1,714 Points

You can not use a decimal as an int.

For instance if you did the following function is_int():

is_int(23) = bool(true)
is_int('23') = bool(false)
is_int(23.5) = bool(false)
is_int('23.5') = bool(false)
is_int(NULL) = bool(false)
is_int(true) = bool(false)
is_int(false) = bool(false)

You will notice that all the decimal numbers are all returning false. The reason a couple are returning false is because when adding an int, you can not have the single quotes, all integers do not need single or double quotes because they are not a string.

Fuzzyclaws Frostpaw
Fuzzyclaws Frostpaw
5,178 Points

PHP auto-handles the "type" by checking the values of the variables... to expand on this further; if you need an integer, you set the variable's value as a whole number if you need a floating point number, you set the variable's value as a decimal. if you need the variable to act as a string to pass on another variable within it, you set the variable's value with double quotes and use concatenation if necessary, otherwise, you can use single quotes.