
Variable – is like a magic box that stores information once the information is stored in the box it can be changed, or taken out to be displayed.
All variable names must be have a dollar sign “$” at the start of the variable.
After the dollar sign “$” the variable name must start with a letter “A-Z” or “a-z” or a underscore “_” it cannot start with a number
After the “$” the rest of the name can be any combination of letters, numbers, and underscores.
Spaces can’t be used in variable names please use underscores “_” to represent spaces
Variable names are case sensitive so “$_cat” and “$_Cat” is different. (Try not to use variable with same names to reduce confusion).
Tips on creating variables
- Always use lowercase on variable names
- Always make your variable name descriptive
- Always comment the purpose of the variables
- Always try to defined all variables before use even though PHP doesn’t require it.
Variable Types
Numbers
There are two types of numbers in PHP Integers and floating-point. Integers are whole numbers that can be both positive or negatives but cannot have fractions of decimals. Floating-point numbers are use to if numbers have decimal places. (The only way to use fraction in PHP is to covert to decimals).
Integer = 1 or -1
Floating-point = 1.02 or -1.02
Strings
A string is any number of characters enclosed in single or double quotation marks ‘string’ or “string”. Strings can be numbers letters symbols and spaces. Stings can also contain variables. Strings can only contain 1 value.
To use quotation marks in a string use the backslash \.
Example: “Yo, “ho ho!”" would have to be written “Yo, \”hoho!\”" in order for it to display properly.
Different quotation marks can be used to fix the problem.
Example: “Yo, ‘ho ho!’” or ‘Yo, “ho ho!”‘.
Arrays
Arrays can contain a list of values so users can put multiple strings and numbers in to one array.
Array uses keys to create and retrieve that values stored. Php has two different types of arrays. An “Indexed array” uses numbers for its keys. An “Associative array” uses strings for its keys. An array containing other arrays is called a “multidimensional array”.
Assigning Values to Variables
To assign a value to a variable the user must use the = sign. The equal sign is called the “assignment operator” because it assigns a value to the variable.
Variable assignment operator value
$string = “Yo Ho!”;
Differences between Quotation marks ‘/”
Single quotation marks ” are treated literally
Double Quotation marks take the value of the variable.
These notes are from PHP for the World Wide Web, Third Edition (Visual QuickStart Guide)
written by Larry Ullman. In order to make sense of my notes you should pruchase that excellent PHP book its very well written and easy to follow.