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 12: Line 12:
  
 
===Examples of Acceptable Variable Names===
 
===Examples of Acceptable Variable Names===
<code>
+
<pre>
 
$var<br />
 
$var<br />
 
$_var<br />
 
$_var<br />
Line 18: Line 18:
 
>$VAR<br />
 
>$VAR<br />
 
$VAR_001<br />
 
$VAR_001<br />
</code>
+
</pre>
  
 
This is not a comprehensive list, merely examples.  
 
This is not a comprehensive list, merely examples.  
Line 24: Line 24:
 
===Examples of Unacceptable Variable Names===
 
===Examples of Unacceptable Variable Names===
  
<code>$var*</code> (Variable names may only contain letters, numbers, and underscores.)<br />
+
<pre>$var*</pre> (Variable names may only contain letters, numbers, and underscores.)<br />
  
<code>$var #1</code> (Variable names may not contain # or spaces.)<br />
+
<pre>$var #1</pre> (Variable names may not contain # or spaces.)<br />
  
<code>var$</code> (Variables names must '''begin''' with a dollar sign.)
+
<pre>var$</pre> (Variables names must '''begin''' with a dollar sign.)
  
  

Revision as of 16:41, 2 August 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. start 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<br />
$_var<br />
$_Var<br />
>$VAR<br />
$VAR_001<br />

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