Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Integers are whole numbers, such as one through nine or negative one through negative nine. Any of these are considered integers because they aren't numbers that include decimal, or floating point, values. We'll be setting, retrieving, preforming operations and functions and displaying these values!
Documentation
Integers can be specified in decimal (base 10), hexadecimal (base 16), octal (base 8) or binary (base 2) notation, optionally preceded by a sign ( or +).
Example Integer literals:
$a = 1234; // decimal number
$a = 123; // a negative number
$a = 0123; // octal number (equivalent to 83 decimal)
$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)
$a = 0b11111111; // binary number (equivalent to 255 decimal)
var_dump() This function displays structured information about one or more expressions that includes its type and value. Arrays and objects are explored recursively with values indented to show structure.
gettype() Returns the type of the PHP variable var. For type checking, use is_* functions.

0:00
The first type of variable we're going to look at is an integer.

0:03
Integers are whole numbers, such as one through nine, or

0:07
negative one through negative nine.

0:09
Any of these are considered integers because they aren't numbers that include

0:14
decimals or floating point values.

0:17
So let's create a couple of integers and see how they work.

0:21
Let's create a new file named numbers.php.

0:29
Remember, we start a PHP block using the opening and closing php tags.

0:35
Now we can create some variables.

0:38
The names don't really matter, but I want to keep it readable, so

0:41
I'll name the first one num_one.

0:45
To store information in this variable,

0:47
we use a single equal sign, followed by the value that we want to store.

0:52
In this case, I'm going to set it equal to one.

0:56
I then end this statement in this line with a semicolon.

1:00
If you notice, I'm not putting quotes around the number one.

1:04
If I were to put quotes around the one,

1:05
PHP would treat this value as a string, not an integer.

1:10
I'll show you the difference between strings and integers here in a minute.

1:13
But let's create a few more, num_two equals two,

1:19
and num_three will equal three.

1:25
So now we have three variables, num_one, num_two, and num_three.

1:28
Let's display these variables using the echo command.

1:31
We’ll echo num_one.

1:37
Then we can run this script.

1:43
We see that it displays the value, which is one.

1:46
Our variable is just a placeholder for the value.

1:49
It's just a one.

1:51
So what about if we were to just echo, say, one, and not the variable?

1:56
Let's also put a one within quotes.

2:00
Now let's run our script again.

2:03
Now we have three ones, and they all look exactly the same.

2:06
So how do we know that something is an integer or something is a string?

2:10
PHP includes some functions that we can use to perform many different tasks,

2:15
such as looking at the details of a variable.

2:19
A function is simply a group of code that performs a function or task.

2:24
We'll learn about writing our own functions in a later course, but

2:27
we can start using them now.

2:30
The first function we'll use is a very handy little function, var_dump.

2:36
The var_dump function accepts data within parentheses and

2:39
dumps the details about that data.

2:42
Instead of echo, let's use var_dump.

2:46
We then surround the data we want to use in parentheses.

2:51
Let's use the var_dump on all three lines.

3:03
Now let's run our script again.

3:07
So you'll notice here, the first two types are int, or integer,

3:11
with the value of one without quotes.

3:14
And the third type is a string with the value of one within quotes.

3:19
So now we can tell that there are actually two different types.

3:23
At this point, it's not a concern that these two values are different types,

3:28
since we're getting the same output either way.

3:31
However, as you'll learn later, sometimes, like when performing a math calculation,

3:35
the type of information stored in a variable really matters.

3:39
So it's good to know that we can test and

3:41
find out whether our variable contains an integer or a string.

3:45
There are other functions you can use with variables.

3:48
So check the teacher's notes for more details.

3:51
Let's duplicate the first var_dump line, and do some math calculations.

3:58
After num_one, I'm going to use an operator that you see all the time,

4:02
the plus sign.

4:04
Then I'm going to type num_two.

4:09
Now let's run the script again.

4:13
We see that the last result is also an integer with a value of three.

4:17
One plus two equals three, awesome.

4:21
By using the plus operator, or

4:23
sum operator, we're able to add two numbers together.

4:27
The numbers are integers, and they're stored in variables,

4:31
so we can change or retrieve them however we'd like.

4:34
Let's do a little bit more here, and say minus.

4:41
And now we'll type num_three.

4:44
We save the file and run the script again.

4:48
That brings us to zero.

4:50
One plus two minus three equals zero.

4:55
Great job storing, retrieving, and manipulating your first set of data.

5:00
That should give you a good understanding of what an integer is and how to use one.

5:05
Not all numbers are integers.

5:07
Oftentimes, you'll need a floating point value, such as 2.25, to represent

5:13
a dollar amount, or other decimal number. Enter our next variable type, floats.
You need to sign up for Treehouse in order to download course files.
Sign up