Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
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).
[SOUND] There are two basic parts to writing code, the storage and retrieval of
data, and the logic that tells what and when to do something with that data.
There are many, many ways to handle these two pieces, and that's one of the things I
enjoy about programming, figuring out the best way to solve a problem.
We'll use both these pieces to create a simple program for
converting units of measurement.
Variables are the most basic way of storing and
keeping track of information in a program.
For example, a computer game keeps track of a player's score.
At the start of the game, the score is zero.
It can go up if the player does well, or go down if the player makes a mistake.
The game might even end if the player reaches a particular score.
Score is an example of a variable.
Although the score's value will change, 0 at the beginning, 100 at the end for
example, it's still always just one score for that player.
Think of a variable as a box.
You can put something inside the box, look inside the box to see what's inside it,
empty the box, and even put something new inside the box.
While the contents of the box changes, it's still always the same box.
A program might need lots of variables, or
boxes, to keep track of lots of information.
In order for our program to follow all those variables,
we need a way to identify each variable in a program.
That's why each variable has its own name, like score,
which identifies that one variable.
Variables in PHP always begin with a dollar sign,
followed immediately by an underscore or a letter, and
then any combination of numbers letters and underscores.
You cannot start a variable with a number.
We could call this variable $score.
Our variable score represents the box.
When you create a variable you can leave it empty.
In that case, add a semicolon to the end of a statement.
Or you can create the variable and put something into it in a single statement.
Using the equal sign, you can insert a value into a variable.
The single equal sign tells PHP to put whatever's on the right side into the name
on the left side.
In this example put zero into score.
When we place something in a variable we call that assigning a value
to the variable.
Throughout this course we'll be covering the four scalar, or base variable types
that are supported in PHP, boolean, integer, float and string.
A boolean value is either true or false.
You'll use these a lot when adding logic to your programs.
Integers are whole numbers such as 1 through 9 or -1 through -9.
A float is a number that uses a decimal place such as storing
the price of an item as 2.25.
A string variable combines any number of characters such as a sentence or
For example, Hello World!
PHP also supports two compound variable types that we'll look at in later courses,
array and object.
These types allow you to collect many different values into a single variable.
You'll be using variables often.
So let's get started exploring how to set, retrieve, and manipulate number variables.
You need to sign up for Treehouse in order to download course files.Sign up