Variables in PHP3:40 with Alena Holligan
Variables are the most basic way of storing and keeping track of information in a program. Throughout this course, we'll be covering the 4 scalar, or base variable types that are supported in PHP: boolean, integer, float and string.
Variables in PHP
The type of a variable is not usually set by the programmer; rather, it is decided at runtime by PHP depending on the context in which that variable is used. PHP supports eight primitive types.
- Four scalar types:
- float (floating-point number, aka double)
- Two compound types:
- And finally two special types:
Why variables can't start with a number.
This is a standard for all programming languages. Primarily to standardize between languages whose variables do not start with a symbol. It is easier for a compiler to make an assumption that if a token begins with a number, it is most likely a numeric value rather than a variable name and any non-numeric characters that are a part of the token would assist in determining the numeric type or number system (12e5 is a valid number).
[MUSIC] 0:00 [SOUND] There are two basic parts to writing code, the storage and retrieval of 0:04 data, and the logic that tells what and when to do something with that data. 0:09 There are many, many ways to handle these two pieces, and that's one of the things I 0:14 enjoy about programming, figuring out the best way to solve a problem. 0:19 We'll use both these pieces to create a simple program for 0:24 converting units of measurement. 0:27 Variables are the most basic way of storing and 0:29 keeping track of information in a program. 0:32 For example, a computer game keeps track of a player's score. 0:35 At the start of the game, the score is zero. 0:39 It can go up if the player does well, or go down if the player makes a mistake. 0:42 The game might even end if the player reaches a particular score. 0:47 Score is an example of a variable. 0:51 Although the score's value will change, 0 at the beginning, 100 at the end for 0:54 example, it's still always just one score for that player. 0:58 Think of a variable as a box. 1:03 You can put something inside the box, look inside the box to see what's inside it, 1:05 empty the box, and even put something new inside the box. 1:10 While the contents of the box changes, it's still always the same box. 1:14 A program might need lots of variables, or 1:18 boxes, to keep track of lots of information. 1:22 In order for our program to follow all those variables, 1:25 we need a way to identify each variable in a program. 1:28 That's why each variable has its own name, like score, 1:32 which identifies that one variable. 1:36 Variables in PHP always begin with a dollar sign, 1:38 followed immediately by an underscore or a letter, and 1:42 then any combination of numbers letters and underscores. 1:46 You cannot start a variable with a number. 1:51 We could call this variable $score. 1:53 Our variable score represents the box. 1:57 When you create a variable you can leave it empty. 2:00 In that case, add a semicolon to the end of a statement. 2:04 Or you can create the variable and put something into it in a single statement. 2:07 Using the equal sign, you can insert a value into a variable. 2:13 $score=0. 2:18 The single equal sign tells PHP to put whatever's on the right side into the name 2:21 on the left side. 2:26 In this example put zero into score. 2:27 When we place something in a variable we call that assigning a value 2:31 to the variable. 2:35 Throughout this course we'll be covering the four scalar, or base variable types 2:37 that are supported in PHP, boolean, integer, float and string. 2:42 A boolean value is either true or false. 2:49 You'll use these a lot when adding logic to your programs. 2:52 Integers are whole numbers such as 1 through 9 or -1 through -9. 2:56 A float is a number that uses a decimal place such as storing 3:01 the price of an item as 2.25. 3:06 A string variable combines any number of characters such as a sentence or 3:09 paragraph. 3:14 For example, Hello World! 3:16 PHP also supports two compound variable types that we'll look at in later courses, 3:19 array and object. 3:25 These types allow you to collect many different values into a single variable. 3:27 You'll be using variables often. 3:32 So let's get started exploring how to set, retrieve, and manipulate number variables. 3:34
You need to sign up for Treehouse in order to download course files.Sign up