1 00:00:00,484 --> 00:00:04,808 [MUSIC] 2 00:00:04,808 --> 00:00:09,464 [SOUND] There are two basic parts to writing code, the storage and retrieval of 3 00:00:09,464 --> 00:00:14,061 data, and the logic that tells what and when to do something with that data. 4 00:00:14,061 --> 00:00:19,052 There are many, many ways to handle these two pieces, and that's one of the things I 5 00:00:19,052 --> 00:00:24,230 enjoy about programming, figuring out the best way to solve a problem. 6 00:00:24,230 --> 00:00:27,360 We'll use both these pieces to create a simple program for 7 00:00:27,360 --> 00:00:28,810 converting units of measurement. 8 00:00:29,820 --> 00:00:32,590 Variables are the most basic way of storing and 9 00:00:32,590 --> 00:00:34,660 keeping track of information in a program. 10 00:00:35,790 --> 00:00:39,890 For example, a computer game keeps track of a player's score. 11 00:00:39,890 --> 00:00:42,340 At the start of the game, the score is zero. 12 00:00:42,340 --> 00:00:47,140 It can go up if the player does well, or go down if the player makes a mistake. 13 00:00:47,140 --> 00:00:51,400 The game might even end if the player reaches a particular score. 14 00:00:51,400 --> 00:00:54,010 Score is an example of a variable. 15 00:00:54,010 --> 00:00:58,880 Although the score's value will change, 0 at the beginning, 100 at the end for 16 00:00:58,880 --> 00:01:02,630 example, it's still always just one score for that player. 17 00:01:03,640 --> 00:01:05,500 Think of a variable as a box. 18 00:01:05,500 --> 00:01:10,560 You can put something inside the box, look inside the box to see what's inside it, 19 00:01:10,560 --> 00:01:14,590 empty the box, and even put something new inside the box. 20 00:01:14,590 --> 00:01:18,470 While the contents of the box changes, it's still always the same box. 21 00:01:18,470 --> 00:01:22,030 A program might need lots of variables, or 22 00:01:22,030 --> 00:01:25,500 boxes, to keep track of lots of information. 23 00:01:25,500 --> 00:01:28,720 In order for our program to follow all those variables, 24 00:01:28,720 --> 00:01:32,360 we need a way to identify each variable in a program. 25 00:01:32,360 --> 00:01:36,350 That's why each variable has its own name, like score, 26 00:01:36,350 --> 00:01:38,356 which identifies that one variable. 27 00:01:38,356 --> 00:01:42,990 Variables in PHP always begin with a dollar sign, 28 00:01:42,990 --> 00:01:46,420 followed immediately by an underscore or a letter, and 29 00:01:46,420 --> 00:01:51,000 then any combination of numbers letters and underscores. 30 00:01:51,000 --> 00:01:53,660 You cannot start a variable with a number. 31 00:01:53,660 --> 00:01:57,370 We could call this variable \$score. 32 00:01:57,370 --> 00:01:59,800 Our variable score represents the box. 33 00:02:00,810 --> 00:02:04,080 When you create a variable you can leave it empty. 34 00:02:04,080 --> 00:02:07,890 In that case, add a semicolon to the end of a statement. 35 00:02:07,890 --> 00:02:13,510 Or you can create the variable and put something into it in a single statement. 36 00:02:13,510 --> 00:02:18,380 Using the equal sign, you can insert a value into a variable. 37 00:02:18,380 --> 00:02:21,550 \$score=0. 38 00:02:21,550 --> 00:02:26,480 The single equal sign tells PHP to put whatever's on the right side into the name 39 00:02:26,480 --> 00:02:27,870 on the left side. 40 00:02:27,870 --> 00:02:31,518 In this example put zero into score. 41 00:02:31,518 --> 00:02:35,940 When we place something in a variable we call that assigning a value 42 00:02:35,940 --> 00:02:37,540 to the variable. 43 00:02:37,540 --> 00:02:42,470 Throughout this course we'll be covering the four scalar, or base variable types 44 00:02:42,470 --> 00:02:48,130 that are supported in PHP, boolean, integer, float and string. 45 00:02:49,230 --> 00:02:52,515 A boolean value is either true or false. 46 00:02:52,515 --> 00:02:56,010 You'll use these a lot when adding logic to your programs. 47 00:02:56,010 --> 00:03:01,932 Integers are whole numbers such as 1 through 9 or -1 through -9. 48 00:03:01,932 --> 00:03:06,853 A float is a number that uses a decimal place such as storing 49 00:03:06,853 --> 00:03:09,673 the price of an item as 2.25. 50 00:03:09,673 --> 00:03:14,586 A string variable combines any number of characters such as a sentence or 51 00:03:14,586 --> 00:03:16,040 paragraph. 52 00:03:16,040 --> 00:03:17,870 For example, Hello World! 53 00:03:19,560 --> 00:03:25,300 PHP also supports two compound variable types that we'll look at in later courses, 54 00:03:25,300 --> 00:03:27,310 array and object. 55 00:03:27,310 --> 00:03:32,380 These types allow you to collect many different values into a single variable. 56 00:03:32,380 --> 00:03:34,380 You'll be using variables often. 57 00:03:34,380 --> 00:03:39,630 So let's get started exploring how to set, retrieve, and manipulate number variables.