PHP – Lesson 02: Data Types

In This Lesson:



What are Data Types

Data Types Defined

A “data type” refers to the type of data a variable can store. There are several different data types PHP can store, but before we dive into those types, let’s first look at what a variable is.

What is a variable?

A variable is a symbolic representation of a value, and the value stored within this symbolic representation can change over time, or “vary.” This ability to morph from one thing to another is what makes variables so useful.

In lay terms, A variable is just a name for something that may change or that which you don’t know in advance. A perfect example of a variable is the English word, “something.” We use that word as a placeholder in a sentence to name a thing unknown. While the “something” is unknown, the understanding is still that it’s some specific thing worthy of bringing up in conversation.

In PHP variables always start with a $ character. So if you were hungry, you could say:

<?php
$something = "cake";
echo "I want to eat $something";
?>

This would output “I want to eat cake” in a browser.

Variable rules

Here are some hard-and-fast rules about variables in PHP:

  • Variables always start with $
  • The first character after $ cannot be a number.
  • No spaces or special characters allowed except for the underscore (eg $my_variable)
  • Variable names are case-sensitive ($myvariable is not the same as $myVariable)
  • You can’t use a variable called $this because $this has special meaning in PHP programming.


Now, onto the types of data PHP can use….

PHP technically has eight (8) different data types you can work with. These are:

  1. integer numbers
  2. floating point numbers
  3. strings
  4. booleans
  5. arrays
  6. objects
  7. resouces
  8. null

These 8 types can fall into 3 basic categories:

  1. Scalar (also referred to as “primitive”): data types that can only hold a single value
    1. integer numbers
    2. floating point numbers
    3. strings
    4. booleans
  2. Compound: data types that store a collection of values
    1. arrays
    2. objects (we will not get much into object-oriented programming in this course!)
  3. Special cases
    1. resouces
    2. null

Explanations of these different data types:

Let’s first start with the special cases: “resources” and “NULL.”

Resources are not an actual data type, but the storing of a reference to functions and resources external to PHP. The most common example of using the resource data type is a database call. We will not talk about them here, since they are an advanced topic.

The NULL data type is a variable that has no value assigned to it, except itself, or the equivalent of the concept of “nothing.” It is not the same as assigning a value of “0” since “0” is an integer, however. It represents the concept of an empty value.

 

Now let’s talk about the Scalar types:

  1. Character string: A series of single characters, such as “hi there”. There is no practical limit on the length of a string.
  2. Integer: A whole number (no fractions), such as –43, 0, 1, 27, or 5438. The range of integers that is allowed varies, depending on your operating system, but in general, you can usually use any number from –2 billion up to +2 billion.
  3. Floating point number: A number (usually not a whole number) that includes decimal places, such as 5.24 or 123.456789. This is often called a real number or a float.
  4. Boolean: A TRUE or FALSE value.

(reference: special thanks to Dummies.com)

Before we continue, let’s quickly address the Compound types of data:

  1. arrays
    • We’ll just touch on this concept but save it for the next lecture since it’s a little bit more advanced. An array is essentially a variable that holds multiple sets of possible values for a single symbolic representation.
    • For instance, let’s say we have the variable of $fruit . Now let’s say that the $fruit array holds the following possible values: “apples”, “oranges”, “grapes, “pears”. At a given time, $fruit might equal any single one of those values.
  2. objects
    • This is a more advanced PHP topic than we will cover in this course. Patience, grasshopper.

But WAIT! A little bit about the Boolean type:

Boolean data types represent two possible states — TRUE or FALSE. Boolean values
are used mainly to compare conditions for use in conditional statements. For example, when PHP evaluates an expression, such as $a > $b (or in plain English, “is $a greater than $b?”), the Boolean outcome is either TRUE or FALSE.

PHP considers the following values FALSE :

  • The actual word, FALSE without quotation marks ( which can be upper- or lowercase)
  • The integer 0
  • The float 0.0
  • An empty string “”
  • The one-character string “0”
  • The constant NULL

Any other values in a Boolean variable are considered TRUE. If you echo a Boolean variable, the value FALSE displays as a blank string; the value TRUE echoes as a 1. Functions often return a Boolean variable that you can test to see whether the function succeeded or failed.


Data Type conversion and assignment

Switching between types on the fly…

Most other languages require that you initialize the variable before using it, specifying what type of data it can hold, but PHP is less formal. You don’t need to tell PHP which data type is in a variable. PHP evaluates the data when you assign it to the variable and then stores it as the appropriate type. This is normally quite helpful. PHP guesses the data type pretty accurately.

PHP also converts data when it needs to be converted. For example, PHP can convers the data types for the following statements without a hitch:

$integerVar = 1; // stored as an integer
$floatVar = 1.1; // stored as as a float
$sumVar = $integerVar + $floatVar; // converts $sumVar to float of 2.1

If PHP didn’t convert data types for us, the third statement would not be possible because the data to be added are different types. However, PHP converts the integer to a float so that the addition proceeds smoothly. This happens automatically and invisibly and is very helpful.

Okay, so how could we make sure about our data types when it matters? ….

Type Casting

Occassionally, PHP can’t read a programmer’s mind when it stores the data. You might need to do something with a variable, and PHP won’t let you because the data is the wrong type. In such a case, you can specify how you want PHP to store the data, rather than let PHP decide for itself. This is called type casting. To specify a particular type, use a statement like one of the following:

$integerVar = (int) $var1;
$floatVar= (float) $var1;
$stringVar = (string) $var1;

The value in the variable on the right side of the equal sign ($var1) is stored in the variable on the left side as the data type specified in the parentheses. So the value in $var1 is stored in $integerVar as an integer, as specified by (int).

Be careful when doing type casts. Sometimes you can get unexpected results. For example, when you cast a float into an integer, it loses its decimal places. To do this, PHP rounds the float toward 0. For example, if $var1 = 1.8 and you cast it into an integer — $integerVar = (int) $var1; — then $integerVar will equal 1 instead of 1.8.

 

For a working example file with comments, download the zipped numbers.php testing file. Unzip it and load it into your web serving development folder.

Video Lecture(s) Version of this Page

Data Type Overview Part 1 – Intro to Variables (8:41)


Data Type Overview Part 2 – Categories (9:48)


Data Type Overview Part 3 – Type Casting (6:04)