Tutorial 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.

<?php
	$logged_in = true;
?>

Integer

An integer variable is any whole number.

<?php
	$fav_num = 2940;
?>

Floating Point

Usually a fractional number, with a decimal.

<?php
	$top_speed = 104.87;
?>

String

Simple text that must be enclosed within double quotations " " or single quotations ' '

<?php
	$vehicle = "Subaru";
?>