Variables illustration

The current date and time is February 4th, 2012 2:15:PM

Companion Web Site

Larry Ullman has a web site devoted to PHP for the World Wide Web: Second Edition at this location.

It is intended as a supplement /complement to the book and has many helpful resources.

The Book

Variable image

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

Chapter 2
Variables

Variables as Containers

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

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:

It is June and the average temperature is 88. We like June!

Types of Variables

Numbers, strings and arrays are types of variables

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:

The address is:
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!


 
Warning: show_source() has been disabled for security reasons in /home/rjoannej/public_html/ullmanBook/chapter2.php on line 203

Warning: show_source() has been disabled for security reasons in /home/rjoannej/public_html/ullmanBook/chapter2.php on line 207