Now that we have a good idea of what variables do, let’s see what data types can be used in regards to utilizing them. Here is a list of data types that PHP supports, and how you can use them:
String: This comprises any text inside quotes and has been experimented with in earlier examples. You can use either double or single quotes when working with them; they both perform the same action.
I added an echo statement in between the two variables so that when it prints off in the browser, they’re both in two different lines.
Integer: This is any whole number, and must follow these rules:
- It has at least one digit
- Cannot have a decimal in it
- Based out of three formats: decimal (base-10), hexadecimal (base-16) or octal (base-8)
A useful thing to remember with integers is that they will read off both positive and negative numbers, as shown in this example.
The function of var_dump() basically acts as a return caller that performs the action of returning the data type and value. You can actually combine different variables to return different values – just like doing math!
Boolean: You can either do true or false for variables with this data type.
Null: This data type can only contain the value “Null”. There are a few unique characteristics of the Null data type:
- Null is case insensitive
- Null is the only value that can be used for the data type
- Null is considered a variable with no value, i.e. no alphanumeric characters can be used for this data type
Object: These are good for storing data internally. Object is also the only data type so far that has to be specifically declared. Here is an example of this being done:
Objects rely on the parent class as well as methods that define the instance of the object that remain independent. There can be multiple independent instances within an object class.
Tip: Using the new statement is used to instantiate a class in PHP, as shown when transforming $doge to create an object.
Check in next time when we go over strings in detail, constants and operators!