PHP for the World Wide Web: Second Edition written by Larry Ullman
Chapter 2
Variables

Chapter 1 was about using PHP to do something that we can already do using HTML. Chapter 2 is where we will be finding out how PHP can make our pages dynamic and interactive. This is where variables come in. Chapter 2 is a good, basic introduction(or reintroduction) to variables.
What are Variables?
If you think of a bucket as a container for stuff, then you will understand the concept of 'variable'. A variable is assigned a value in a script in this way: $container=bucket
PHP has predefined variables and we can see what they are by running a script on the server using the variable $GLOBALS. The script will be activated by clicking $GLOBALS. This script is handy for debugging scripts because it contains the variables that exist and what their values are. The <pre></pre> tags format the print out so that it is easy to read.
Variable Syntax
All variables
- begin with $
- then a letter (upper or lower), or_ but never with a number
- can be a combination of any of the above including numbers
- are case sensitive
- are assigned value with =
- have no spaces
Using Camel formatting has become a really good way for me for naming variables, such as $myName along with using sensible file names. Here is a simple script with variables:
Types of Variables
Numbers, strings and arrays are types of variables
- Strings can be combinations of everything
(letters, numbers, symbols, variables)and are
en
closed in quotation marks - Numbers are either integers or decimals and are not enclosed in quotes
- Arrays are lists of values and are indexed by keys which can be numbers or strings
When an array contains other arrays, it is a multidimensional array.
Here are the variables' types, in detail, with a var_dump ($GLOBALS);!
It is similar to the $GLOBALS.
var_dump ($GLOBALS);
Someday I might be able to understand what all that means!
Assigning Values to Variables
Varaiables are assigned their value with the use of what
is called the 'assignment operator'.
We know it in another life as the equal sign.
A typical variable assignment using correct
syntax would look like this:
$myName= "Joanne Johnson"; and to print
it to the page I would type this:
echo ""My name is $myName;" within a
set of PHP tags.
Here is a simple multi-lined script:
Park Avenue in Providence, Rhode Island.
The zip is 12345.
Quotation Marks
Double quotation marks print out the variable and single quotation marks print out the literal information enclosed within. Here is an example:
Single Quotes: name1 is $firstName $lastName
Double Quotes: name2 is Joanne Johnson
I had difficulty getting \n to work, but in looking around the web found that the very same thing can be accomplished with HTML in the code. So, <br /> will do it for me! However, I do understand escaping certain things like ' or".
\ is now set in my mind as it should be! I had, for whatever reason, seen it as a forward slash and it isn't! It is the backward slash.
The discussion among Bonnie, Krisse and me was very heated and we ended up learning something we won't forget! Inorder to escape something like n or r or ", it must be excepted while between double quotes.
On to Chapter 3!

