Thursday, March 8, 2012

Getting Started with PHP: Understanding Variables

Variables


Programming languages work with the computer to retrieve data from the computer's RAM (Random Access Memory). As I explained in the first section, PHP pulls information from a database using user input. In order to do this successfully, PHP, like many programming langauges relies on the existence of variables.







Variables split data into organizational blocks known as values, allowing programs to speedily update information and perform multiple processes.


Think of a variable as a box with a ball inside of it.
The ball of data inside of this variabledetermines the variable's value.
Without a value, a variablemeans nothing, in PHP we can simultaneously
declare the valueof a variable. You can instantly peer inside of the
box and see exactly what it means.
You can think of a variable as a container, where values are stored. Data types contained within them determine a variable's value.
Learning to work with and understand variables is a basic skill you'll need to master in programming. With the power of variables, you can create programs that execute simple and complex functions. In PHP, you can create and declare variables in a single-step, let's look at a basic variable declaration in PHP:

$userName = "AwesomeDoni";

Note the use of the $ symbol to indicate the beginning of the variable. This is a coding rule unique to PHP. This simple variable tells the computer two things: 1.) it initializes the variable called userName and 2.) gives it a value, the text string AwesomeDoni.

Basic Rules for Variables

Some basic rules for variables:

  • PHP reads variables as a single word
  • They contain no spaces or dashes but can be distinguished by underscores or capital letters:
    • $blackCatsWithWhiteStripes (this is called camelCase)
    • $black_cats_with_white_stripes
  • PHP variables are case sensitive
  • PHP uses three types of variables: local, global and property
Variables can hold numeric values and names.

Therefore, you can declare a variable like this:

$hotTacoSauce = 10;
or this:
$BlackKittens = "cute"; 

These are both legitimate declarations of variables in PHP.

Questions?

No comments:

Post a Comment