(click anywhere to close)

[PHP] Data Types

category: Website | course: PHP | difficulty:

When programming, everything you do is simply doing stuff with data. This might sound scary, but hear me out. The purpose of any program is to present information to the user, or upload information from the user to the server. You could, for example, calculate the costs of buying everything inside a shopping cart, or dynamically create an HTML navigation from pieces of data, or load all the text from a blog post.

Therefore, everything your script will work with has to be data of some kind, which is why PHP has several basic data types. The eight types I will discuss here are called primitives.

Within the primitives category, there’s another distinction made between scalar types, compound types and special types.

Scalar Types

Scalar types contain data with only one value; they are the fundamental building blocks of the programming language. There’s nothing smaller than this.

Integer

An integer is a whole number (positive or negative). It can be entered just like you’d expect: by typing the actual number. Alternatively, you can enter the number in hexadecimal, octal, or binary format.

```1234; // Decimal number
-123; // Negative number
0123; // Octal number
0x1A; // Hexadecimal number
0b11111111; // Binary number
```

Of course, integers can’t get infinitely large, so there’s a maximum value. This is platform-dependant, but usually around two billion. Once you go beyond that, PHP will convert the value to a float.

Float

A floating point number (or double) is any real number, including integers, but also fractions and anything else you can imagine. There are two ways to write them; the regular notation (with the decimal point), and scientific notation.

```1.234; //Regular floating point
1.2e3; //Scientific notation for 1200
7E-2;  //Scientific notation for 0.07
```

Floating numbers have a maximum as well, which lies around `1.8e308` (which is a very, very big number).

Also keep in mind, that computers work with a base 2 system, which means that numbers that look nice in our base 10 system (such as 0.1 or 0.55), will have very tiny rounding errors when you start working with them. Usually, this won’t be a problem, as long as you never exactly compare two floating numbers.

Boolean

A Boolean is the simplest type of them all. It has only two values: `true` or `false`. Both are case-insensitive. You can use it like an on-off switch; if a certain condition is true, execute this piece of code, if it’s false instead, execute this other piece.

```true
false
```

String

A string is a sequence of characters, usually used for pieces of text. Strings can get as large as more than two billion characters. There are four ways to create a string: single quotes, double quotes, heredoc and nowdoc.

Single Quotes vs Double Quotes

Placing a piece of text between single (`' '`) or double (`" "`) quotes turns it into a string. Don’t mix them up, though, or you will meet many errors.

```'This is a cool string';
"This is a cool string as well";

"This is a cool string';
//ERROR, single quotes can't close off double quotes
```

Heredoc vs Nowdoc

For very long pieces of text, spanning multiple lines, the other two syntaxes come in handy. Instead of quotes, you can choose the delimiters yourself!

The heredoc syntax looks like this

```<<<DELIMITER some long string DELIMITER; ```

The nowdoc syntax like this

```<<<'DELIMITER' some long string DELIMITER; ```

The difference is that the second syntax does not parse the string. If you were to add variables or expressions within the string (which you’ll learn about soon), they would not be parsed, but simply displayed as text instead.

```\$panda = 'be';

<<<PANDA
To
be
or
not
to
\$panda
PANDA;
//Prints: To be or not to be

<<<'PANDA'
To
be
or
not
to
\$panda
PANDA;
//Prints: To be or not to \$panda
```

It’s very important that the heredoc and nowdoc closing delimiter does not have any other characters on its line – not even spaces or indentation.

Compound Types

Compound types are essentially a collection of scalar types. I’ll explain these more in-depth in their respective chapters.

Array

An array is a list of values (of different data types). You could, for example, store all the pages a user has visited as strings in an array.

```\$grocery_list = ["Bread", "Milk", "Sugar", "Honey", "Cornflakes", "Bicycle", "iPad", "Ferrari"];
```

Object

An object is an expansion on the array. Instead of only containing values, it can also have functions you can execute. Whereas an array is just a list, an object is used for things that actually represent objects. For example, you could create a single new object for every item a user has in his/her shopping cart, and let that object hold all the information about that item.

```//This is just to give you a general idea, you don't need to understand it now
\$new_user = new User(); //Create a new user object
\$new_user->log_in();    //Call a function on the object
```

Special Types

Special types are, well, special exceptions.

NULL

Sometimes you’re going to work with data that doesn’t have a determined data type (yet), or data that has been removed. For these cases, we say the data is of type null. The only value for this data type, is `null`. (Which is case-insensitive, just as with Booleans.)

```null;
```

Resource

A resource is a reference to an external resource. They can only be created by a small list of special functions. Because there’s a huge amount of different resources you could access and work with in PHP, I won’t mention them all in this course. I will discuss only the most important ones: functions that access a database to get some data from it, or upload data to it.

```//For example, the following code connects to a database and returns a resource
mysqli_connect("localhost", "my_user", "my_password", "my_db");
```
CONTINUE WITH THIS COURSE
Do you like my tutorials?
To keep this site running, donate some motivational food!
Crisps
(€2.00)
Chocolate Milk
(€3.50)
Pizza
(€5.00)