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
m (Added "Needs Attention" template)
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
{{Template:Needs Attention}}
 +
{{Template:PHP4 5}}
 
[[Category:PHP]]
 
[[Category:PHP]]
[[Category:Articles_needing_attention]]
 
 
== Basics ==
 
== Basics ==
  
 
Variables in PHP:
 
Variables in PHP:
  
#always begin with a dollar sign ($)
+
*are always preceded by a dollar sign (<span class="mono">$</span>)
#starts with a letter or underscore (_)
+
*start with a letter or underscore (<span class="mono">_</span>)
#may contain any number of letters, numbers, and underscores
+
*may contain any number of letters, numbers, and underscores
#are case-sensitive
+
*are case-sensitive
  
===Acceptable Variable Names===
+
===Examples of Acceptable Variable Names===
 
+
<pre>
<nowiki>$var
+
$var
 
$_var
 
$_var
 
$_Var
 
$_Var
 
$VAR
 
$VAR
$VAR_001</nowiki>
+
$VAR_001
 +
</pre>
 +
 
 +
This is not a comprehensive list, merely examples.
  
This is not a comprehensive list, merely examples.
+
===Examples of Unacceptable Variable Names===
  
===Unacceptable Variable Names===
+
<pre>$var*</pre> (Variable names may only contain letters, numbers, and underscores.)<br />
  
<nowiki>$var*</nowiki> (Variable names may only contain letters, numbers, and underscores.)
+
<pre>$var #1</pre> (Variable names may not contain # or spaces.)<br />
<nowiki>$var #1</nowiki> (Variable names may not contain # or spaces.)
 
<nowiki>var$</nowiki> (Variables names must '''begin''' with a dollar sign.)
 
  
 +
<pre>var$</pre> (Variables names must '''begin''' with a dollar sign.)
  
 
==Types of Variables==
 
==Types of Variables==
Line 33: Line 36:
 
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>
+
<pre>$var = "Hello, world."</pre>

Latest revision as of 18:02, 8 July 2017

Farm-Fresh cup edit.png

Needs Attention
This article needs attention; however, it is not necessarily a stub or an orphan.

Php-med-trans.png

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

Basics

Variables in PHP:

  • are always preceded by a dollar sign ($)
  • start with a letter or underscore (_)
  • may contain any number of letters, numbers, and underscores
  • 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."