Tuesday, October 22, 2013

Declaring variables in PHP

Something we need to fully understand when learning any programming language, is how to use variables. This might sound simple but even when it is, every program needs variables to store data we use.

How do variables work? and how do I declare one?

To declare them it’s simple, you only have to create them by giving them a proper name that follows these rules.
  • always start the variable with the ‘$’ sign
  • exactly after the dollar sign, type any letter or underscore ‘_’
  • limit your variable’s name to alphanumeric characters(beside the first $ character)
Here’s an example, well, examples…
01<?php
02
03//Correct variable declarations
04$_first = "this is my first variable";
05$another2 ="my second variable";
06
07//incorrect variable declarations
08$3var = "a third variable?"; // incorrect declaration, never use a number after the $
09variable4 = 4; //incorrect because didn't starteed with the $ sign
10
11?>

Now if you want, you may practice more visually by watching this video tutorial on variable declarations.
Variable’s video tutorial
Well everybody, that’s all for today’s lesson, thank you for passing by and please don’t forget to comment!!