Lesson 2: Variables
Think of a variable as a bucket. Literally, a bucket. That bucket can hold stuff in it —
like food, or dirt. In PHP, variables are buckets, but instead of holding food or dirt, they
hold information — like numbers, text, images, or logic! All you need to know at this
point is that variables are buckets that store information for later use. We'll get to how
we
actually
use
the
variables in an upcoming lecture.
The basic syntax of a variable is a dollar sign ($) directly followed by a variable name
(using text, with no spaces), then an equal sign, followed by the contents of the
variable, ending with a semi-colon. Ex: $variable_name
=
'my
first
variable';
There are 4 basic variable types, and each type of variable (or "bucket") is meant to
hold specific information.
Boolean
A boolean variable specifies a value of true or false.
1 <?php ?
2
$logged_in
=
true;
3 ?>
Integer
An integer variable is any whole number.
1 <?php ?
2
$fav_num
=
2940;
3 ?>
Floating Point
Usually a fractional number, with a decimal.
1 <?php ?
2
$top_speed
=
104.87;
3 ?>
String
Simple text that must be enclosed within double quotations " " or single quotations ' '
1 <?php ?
2
$vehicle
=
"Subaru";
3 ?>
Previous Lecture Next Lecture
Code
Dynamic
Websites
with
PHP
Copyright ©2014 Brad Hussey of CodeCollege.ca