Difference between revisions of "Variables in PHP"
From OdleWiki
Line 12: | Line 12: | ||
===Acceptable Variable Names=== | ===Acceptable Variable Names=== | ||
− | <nowiki>$var | + | <nowiki>$var<br /> |
− | $_var | + | |
− | $_Var | + | $_var<br /> |
− | $VAR | + | |
+ | $_Var<br /> | ||
+ | |||
+ | $VAR<br /> | ||
+ | |||
$VAR_001</nowiki> | $VAR_001</nowiki> | ||
Line 22: | Line 26: | ||
===Unacceptable Variable Names=== | ===Unacceptable Variable Names=== | ||
− | <nowiki>$var*</nowiki> (Variable names may only contain letters, numbers, and underscores.) | + | <nowiki>$var*</nowiki> (Variable names may only contain letters, numbers, and underscores.)<br /> |
− | <nowiki>$var #1</nowiki> (Variable names may not contain # or spaces.) | + | |
+ | <nowiki>$var #1</nowiki> (Variable names may not contain # or spaces.)<br /> | ||
+ | |||
<nowiki>var$</nowiki> (Variables names must '''begin''' with a dollar sign.) | <nowiki>var$</nowiki> (Variables names must '''begin''' with a dollar sign.) | ||
Revision as of 15:16, 30 July 2012
Contents
Basics
Variables in PHP:
- always begin with a dollar sign ($)
- starts with a letter or underscore (_)
- may contain any number of letters, numbers, and underscores
- are case-sensitive
Acceptable Variable Names
$var<br /> $_var<br /> $_Var<br /> $VAR<br /> $VAR_001
This is not a comprehensive list, merely examples.
Unacceptable Variable Names
$var* (Variable names may only contain letters, numbers, and underscores.)
$var #1 (Variable names may not contain # or spaces.)
var$ (Variables names must begin with a dollar sign.)
Types of Variables
Assigning Values to Variables
To assign a value to a variable, use a single equal sign (=)
$var = "Hello, world."