Learn PHP - Lesson 2 Tutorial - What are variables in PHP - Part 1, What is a variable?
Bookmark and Share

Learn PHP - Lesson 2 Tutorial - What are variables in PHP - Part 1, What is a variable?

Lesson 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 1, What is a variable?

Imagine you're creating dinner for yourself.  Let's say you're making spaghetti.  Now I'm not the fancy one so let's say your sauce is a simple meat sauce out of the can, your noodles are a simple spaghetti noodle, and to finish you buy some lettuce and veggies to make a salad.

Think of each item you purchased at the store.  Each item came in it's own container:
The noodles came in the 'cardboard box' container 
The sauce came in the 'aluminum can' container 
The lettuce and veggies came in the 'plastic bag' container 

Well they don't stay in those containers for consumption right:
So the noodles go from the 'cardboard box' container to the 'hot boiling water pan' container, to the 'strainer' container, and finally to the 'plate' container
Same with the sauce. From the 'aluminum can' container to the 'hot sauce pan' container, to the 'plate'container
And again, similarly with the veggies and lettuce from 'plastic bag' container,  to the 'strainer' container, and finally to the 'plate' container

To create the spaghetti plate you passed all your elements to through many different types of containers.

Imagine variables in PHP are just like those containers. They hold all sorts of information, there are different elements held in variables, and they are the vehicle for manipulating content in PHP.


A variable in PHP is set using the $ (dollar) sign.


Let's get into an example using syntax we've learned so far:

 alter index.php in your root directory to look as follows:

<?php


echo 'Hello World <br />';

$a = 'Hello World again';

echo $a;


?>

save and open in your browser:

Learn PHP - Lesson 2 Tutorial - What is a variable


Go to Part 2

« Back to Lesson 1


Recent Comments