Learn PHP - Lesson 2 Tutorial - What are variables in PHP - Part 2, Different types of PHP variables
Bookmark and Share
 Learn PHP - Lesson 2 Tutorial - What are variables in PHP - Part 2 of 5, Different types of PHP variables?
Lesson 2 Overview
In this lesson, we will cover the following: 

1) What a variable is.
2) Different types of variables
3) Type casting | Type juggling variables in PHP
4) Global variables in PHP
5) Advanced applications of a variable you will one day need to know.

Part 2, Different types of php variables?

Going back to our dinner examples in part 1, I introduced you to a bunch of different containers right?

There was the cardboard box container for the noodles, the aluminum can container for the spaghetti sauce, and the plastic bag container for the veggies. 

Here are the different variable types in PHP -

String variable:

$a = 'Hello World';

In part 1 of this lesson, we created a variable $a and assigned it a string (text).  PHP automatically set $a to be a string variable.

To set a string variable, assign a variable a value using single quotes (''), or double quotes(""). We'll talk about the difference between the two in Part 4.

Integer variable:

$a = 503;

Pretty self explanatory, assign a number to a variable and PHP automatically sets that variable to be a integer (number) variable.

NOTE, if you put single, or double quotes around the example above ($a = '503';), PHP will set that variable to be a string instead of an integer.

Boolean variable:

$a = TRUE;
$b = FALSE;

Sometimes you may want to set a trigger to be true or false and build logic around it. This would be considered a Boolean variable.

NULL variable:

$a = NULL;

A variable can have a null (empty) value. This is useful when looping through values and/or building logic around setting a variable.  No worries, we'll get to this later.

Array variable:

$a = array( Honda => 'Civic', Toyota => 'Camry');

Think of an array like a bookshelf.  You can have one row of books, or 5 rows of books. An array is a weigh to store a lot of data in one variable.

NOTE: You can also have a multi-dimensional array.  So, instead of 1 bookshelf with 5 rows of books, you have 10 bookshelves with 5 rows of books.

Arrays are one of my favorite subjects in PHP, we'll talk about them more later.

These are the basic variable types in PHP.

There are other, more advance variable types and we'll touch on them... eventually.  If you'd like to learn about the other variable types now, here they are with their links to their definitions on PHP.net
- Floating Point
- Objects
- Pseudo Types
- Resources

EXAMPLES (edit index.php from previous lessons, save, and view):


// String example
echo 'Hello World <br />';
$a = 'Hello World again<br />';
echo $a;

// Integer example
$b = 10;
$c = 20;
$d = $b + $c;
echo $b.'<br />';
echo $c.'<br />';
echo $d.'<br />';

// Boolean example
$e = TRUE;
if($e) {
echo 'example of True<br />';

// Array example
$f = array( Honda => 'Civic', Toyota => 'Camry');
echo '<br />';
echo $f[Honda];

Recent Comments