Integers5:19 with Alena Holligan
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!
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.
The first type of variable we're going to look at is an integer. 0:00 Integers are whole numbers, such as one through nine, or 0:03 negative one through negative nine. 0:07 Any of these are considered integers because they aren't numbers that include 0:09 decimals or floating point values. 0:14 So let's create a couple of integers and see how they work. 0:17 Let's create a new file named numbers.php. 0:21 Remember, we start a PHP block using the opening and closing php tags. 0:29 Now we can create some variables. 0:35 The names don't really matter, but I want to keep it readable, so 0:38 I'll name the first one num_one. 0:41 To store information in this variable, 0:45 we use a single equal sign, followed by the value that we want to store. 0:47 In this case, I'm going to set it equal to one. 0:52 I then end this statement in this line with a semicolon. 0:56 If you notice, I'm not putting quotes around the number one. 1:00 If I were to put quotes around the one, 1:04 PHP would treat this value as a string, not an integer. 1:05 I'll show you the difference between strings and integers here in a minute. 1:10 But let's create a few more, num_two equals two, 1:13 and num_three will equal three. 1:19 So now we have three variables, num_one, num_two, and num_three. 1:25 Let's display these variables using the echo command. 1:28 We’ll echo num_one. 1:31 Then we can run this script. 1:37 We see that it displays the value, which is one. 1:43 Our variable is just a placeholder for the value. 1:46 It's just a one. 1:49 So what about if we were to just echo, say, one, and not the variable? 1:51 Let's also put a one within quotes. 1:56 Now let's run our script again. 2:00 Now we have three ones, and they all look exactly the same. 2:03 So how do we know that something is an integer or something is a string? 2:06 PHP includes some functions that we can use to perform many different tasks, 2:10 such as looking at the details of a variable. 2:15 A function is simply a group of code that performs a function or task. 2:19 We'll learn about writing our own functions in a later course, but 2:24 we can start using them now. 2:27 The first function we'll use is a very handy little function, var_dump. 2:30 The var_dump function accepts data within parentheses and 2:36 dumps the details about that data. 2:39 Instead of echo, let's use var_dump. 2:42 We then surround the data we want to use in parentheses. 2:46 Let's use the var_dump on all three lines. 2:51 Now let's run our script again. 3:03 So you'll notice here, the first two types are int, or integer, 3:07 with the value of one without quotes. 3:11 And the third type is a string with the value of one within quotes. 3:14 So now we can tell that there are actually two different types. 3:19 At this point, it's not a concern that these two values are different types, 3:23 since we're getting the same output either way. 3:28 However, as you'll learn later, sometimes, like when performing a math calculation, 3:31 the type of information stored in a variable really matters. 3:35 So it's good to know that we can test and 3:39 find out whether our variable contains an integer or a string. 3:41 There are other functions you can use with variables. 3:45 So check the teacher's notes for more details. 3:48 Let's duplicate the first var_dump line, and do some math calculations. 3:51 After num_one, I'm going to use an operator that you see all the time, 3:58 the plus sign. 4:02 Then I'm going to type num_two. 4:04 Now let's run the script again. 4:09 We see that the last result is also an integer with a value of three. 4:13 One plus two equals three, awesome. 4:17 By using the plus operator, or 4:21 sum operator, we're able to add two numbers together. 4:23 The numbers are integers, and they're stored in variables, 4:27 so we can change or retrieve them however we'd like. 4:31 Let's do a little bit more here, and say minus. 4:34 And now we'll type num_three. 4:41 We save the file and run the script again. 4:44 That brings us to zero. 4:48 One plus two minus three equals zero. 4:50 Great job storing, retrieving, and manipulating your first set of data. 4:55 That should give you a good understanding of what an integer is and how to use one. 5:00 Not all numbers are integers. 5:05 Oftentimes, you'll need a floating point value, such as 2.25, to represent 5:07 a dollar amount, or other decimal number. Enter our next variable type, floats. 5:13
You need to sign up for Treehouse in order to download course files.Sign up