This wiki is for the exclusive use of my friends and colleagues. Account creation and anonymous editing have been turned off.

If you are a friend or colleague of mine, and would like to participate in this project, please send me a message.

If you find these pages useful, please donate to help cover server costs. Thanks!

Difference between revisions of "Variables in PHP"

From OdleWiki
Line 11: Line 11:
 
#are case-sensitive
 
#are case-sensitive
  
===Acceptable Variable Names===
+
===Examples of Acceptable Variable Names===
 
+
<code>
<nowiki>$var</nowiki><br />
+
$var<br />
 
+
$_var<br />
<nowiki>$_var</nowiki><br />
+
$_Var<br />
 
+
>$VAR<br />
<nowiki>$_Va</nowiki>r<br />
+
$VAR_001<br />
 
+
</code>
<nowiki>$VAR</nowiki><br />
 
 
 
<nowiki>$VAR_001</nowiki>
 
  
 
This is not a comprehensive list, merely examples.  
 
This is not a comprehensive list, merely examples.  
  
===Unacceptable Variable Names===
+
===Examples of Unacceptable Variable Names===
  
<nowiki>$var*</nowiki> (Variable names may only contain letters, numbers, and underscores.)<br />
+
<code>$var*</code> (Variable names may only contain letters, numbers, and underscores.)<br />
  
<nowiki>$var #1</nowiki> (Variable names may not contain # or spaces.)<br />
+
<code>$var #1</code> (Variable names may not contain # or spaces.)<br />
  
<nowiki>var$</nowiki> (Variables names must '''begin''' with a dollar sign.)
+
<code>var$</code> (Variables names must '''begin''' with a dollar sign.)
  
  
Line 40: Line 37:
 
To assign a value to a variable, use a single equal sign (=)
 
To assign a value to a variable, use a single equal sign (=)
  
<nowiki>$var = "Hello, world."</nowiki>
+
<code>$var = "Hello, world."</code>

Revision as of 17:16, 31 July 2012

Php-med-trans.png

This page describes documentation valid for PHP 4 and PHP 5.

Basics

Variables in PHP:

  1. always begin with a dollar sign ($)
  2. starts with a letter or underscore (_)
  3. may contain any number of letters, numbers, and underscores
  4. are case-sensitive

Examples of Acceptable Variable Names

$var
$_var
$_Var
>$VAR
$VAR_001

This is not a comprehensive list, merely examples.

Examples of 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."