PHP for the World Wide Web (Visual QuickStart Guide)
Authors: Ullman L.
Published year: 2001
Pages: 21-23/116
Buy this book on amazon.com >>
I l @ ve RuBoard

Chapter 2. Variables

In the last chapter you used PHP to send simple text and HTML code to a Web browser ”in other words, something for which you don't need PHP at all! Don't worry, though, as this book will teach you how to use the print() statement in conjunction with other PHP features to do truly useful things with your Web site.

In order to make the leap from creating simple, static pages to dynamic Web applications and interactive Web sites, you need to be able to handle data, for starters. What you will use to accomplish this are variables. Variables are an important concept and an essential tool for PHP, as well as JavaScript, Java, Perl, or any other programming language.

Variables allow you to temporarily store and manipulate data. They give any programming language its true power. Understanding what a variable is, the types of variables that a language supports, and how to use variables is critical to your work. This chapter will discuss the fundamentals of variables used by PHP, while Chapters 4 through 6 will cover what you can specifically do with the different types of variables.

I l @ ve RuBoard
I l @ ve RuBoard

What are Variables ?

A variable is best thought of as a container for data. Once data has been stored in a variable (or, put differently, once a variable has been assigned a value), that data/variable can be altered , printed to the Web browser (when I say printed, it may help to think of it as sent, but it's the print statement that does the sending, so either term is appropriate), saved to a database, e-mailed, and so forth.

Variables are, by their nature, flexible: you can put data into a variable, retrieve that data from it (without affecting the value of the variable itself), put new data in, and you can continue this cycle as long as is necessary. But, variables in PHP are also temporary: they only exist ”that is, they only have a value ”while they are used within a script. Once you are in a new page, those variables cease to exist, unless you pass them along to the new page, which I'll discuss in the next chapter (HTML Forms and PHP).

I l @ ve RuBoard
I l @ ve RuBoard

Variable Syntax

In PHP, all variables begin with a dollar sign ($), followed by the variable name itself. This name must begin with either a letter (A “Z, a “z) or the underscore (_), followed by any number of letters , underscores, or numbers , used in combination or not. You may not use spaces within the name of a variable. Instead, the underscore is commonly used to separate words in a variable name.

Script 2.1. It is always better to include too many comments than too few because what seems obvious at the time of initial programming may not be so understandable when you go back in months later.

graphics/02sc01.jpg

Keep in mind that variables are case-sensitive. Consequently, "$variable" and "$Variable" are two different constructs, although it would never make sense to use two variables with such similar names. One should quickly get into the habit of creating variable names that make sense on their own, as well as using comments to indicate the purpose of variables (Script 2.1). These habits will reduce errors and make revisiting your work less taxing. For example, "$FirstName" is more useful than "$FN" and putting in a comment that details what a variable's purpose is will make your work abundantly clear. In fact, you may decide that "$first_name" is a better variable name than "$FirstName" because there are no capital letters to get right and the words are separated for clarity. No matter how you decide to name your variables, the most important thing to remember is that whatever convention you use, be consistent. This will help you avoid making trivial errors in your programming.

Unlike some other languages, in PHP you neither have to declare what a variable is (to declare a variable is to assign it a type ”I'll cover variable types in Types of Variables ) nor initialize it prior to first use (to initialize a variable is to create it). With PHP, a variable exists and is defined the first time you use it.

I l @ ve RuBoard
PHP for the World Wide Web (Visual QuickStart Guide)
Authors: Ullman L.
Published year: 2001
Pages: 21-23/116
Buy this book on amazon.com >>