Data TypesLesson 7
Author : Mike Dane
Last Updated : November, 2017
As we write more and more complex programs in PHP, we'll want to work with different types of data (data types). Maybe in one program we'll print out someone's favorite movie, in another we'll keep track of how much money a particular product costs, and maybe in another we'll ask someone if they're an organ donor. In everyday life we deal with all different types of data, so why should it be any different for programs?
Many programming languages, including PHP allow you to work with and keep track of only a specific few types of data. Now that might not sound like a lot, but when you think about it, almost all data can be broken down into one of three data types.
- Text (often called strings or chars) - Names, addresses, the text on this page, etc.
- Numbers - Monetary amounts, dates, distances, etc.
- integers - whole numbers and
- floats (also called doubles) - decimal numbers
- True/False Values (often called booleans) - If someone is an Amazon Prime member, if someone is in the military, if a tv show has been cancelled, etc. This type of data may be less intuitive to a new programmer, but trust me you'll learn to love true/false logic.
Now because these are three distinct types of information, PHP is going to handle them a bit differently. There will be certain things we can do with numbers that we can't do with strings or text (like division, multiplication, etc). There will also be a whole range of things we can do with boolean values (true/false) that we can't do with strings or numbers.
Anytime you're writing a program that's keeping track of different pieces of information, you always want to be aware of what data types you're using. For more specific information about how these data types work, and how to use them in PHP, check out the video above where we'll dive into them more specifically!
$name = "Mike"; // Strings $age = 30; // Integer $gpa = 3.5; // Decimal $isTall = true; // Boolean -> true/false $name = "John"; echo "Your name is $name <br>";