A large part of writing scripts, and for that matter programming in general, involves the handling and manipulation of data. Data can take many forms, ranging from single characters, words and sentences to integers, floating point numbers and true and false values. In object oriented environments such as PHP, data can also take the form of an ''object'', a self contained module which is capable of encapsulating any number of data values of numerous different types.
 
A large part of writing scripts, and for that matter programming in general, involves the handling and manipulation of data. Data can take many forms, ranging from single characters, words and sentences to integers, floating point numbers and true and false values. In object oriented environments such as PHP, data can also take the form of an ''object'', a self contained module which is capable of encapsulating any number of data values of numerous different types.
   −
When working with data values in PHP we need some convenient way to store these values so that we can easily access them and make reference to them whenever necessary. This is where PHP variables come in.
+
When working with data values in PHP, we need some convenient way to store these values so that we can easily access them and make reference to them whenever necessary. This is where PHP variables come in.
    
It is often useful to think of variables as computer memory locations where data is to be stored. When declaring a variable in PHP it is assigned a name that can used to reference it in other locations in the PHP script. The value of the variable can be accessed, the value can be changed, and the type of variable can be altered all by reference the name assigned at variable creation time.
 
It is often useful to think of variables as computer memory locations where data is to be stored. When declaring a variable in PHP it is assigned a name that can used to reference it in other locations in the PHP script. The value of the variable can be accessed, the value can be changed, and the type of variable can be altered all by reference the name assigned at variable creation time.
 
== Naming and Creating a Variable in PHP ==
 
== Naming and Creating a Variable in PHP ==
   −
Before learning how to declare a variable in PHP it is first important to understand some rules about variable names (also known as ''variable naming conventions''). All PHP variable names must be pre-fixed with a ''$''. It is this prefix which informs the PHP pre-processor that it is dealing with a variable. The first character of the name must be either a letter or an underscore (_). The remaining characters must comprise only letters, numbers or underscores. All other characters are deemed to be invalid for use in a variable name.
+
Before learning how to declare a variable in PHP it is first important to understand some rules about variable names (also known as ''variable naming conventions''). All PHP variable names must be pre-fixed with a ''$''. It is this prefix which informs the PHP pre-processor that it is dealing with a variable. The first character of the name must be either a letter or an underscore (_). The remaining characters must comprise only of letters, numbers or underscores. All other characters are deemed to be invalid for use in a variable name.
    
Let's look at some valid and invalid PHP variable names:
 
Let's look at some valid and invalid PHP variable names:
 
</pre>
 
</pre>
   −
The examples we have used for accessing varaible values are straightforward because we have always had a space character after the variable name. The question arises as to waht should be done if we need to put other characters immediately after the variable name. For example:
+
The examples we have used for accessing variable values are straightforward because we have always had a space character after the variable name. The question arises as to what should be done if we need to put other characters immediately after the variable name. For example:
    
<pre>
 
<pre>
 
== Checking Whether a Variable is Set ==
 
== Checking Whether a Variable is Set ==
   Exception encountered, of type "Error"
[279cfc77] /index.php?diff=prev&oldid=1449&title=An_Introduction_to_PHP_Variables Error from line 434 of /var/www/techotopia/includes/diff/DairikiDiff.php: Call to undefined function each()
Backtrace:
#0 /var/www/techotopia/includes/diff/DairikiDiff.php(544): DiffEngine->diag()
#1 /var/www/techotopia/includes/diff/DairikiDiff.php(344): DiffEngine->compareSeq()
#2 /var/www/techotopia/includes/diff/DairikiDiff.php(227): DiffEngine->diffLocal()
#3 /var/www/techotopia/includes/diff/DairikiDiff.php(721): DiffEngine->diff()
#4 /var/www/techotopia/includes/diff/DairikiDiff.php(859): Diff->__construct()
#5 /var/www/techotopia/includes/diff/DairikiDiff.php(980): MappedDiff->__construct()
#6 /var/www/techotopia/includes/diff/TableDiffFormatter.php(194): WordLevelDiff->__construct()
#7 /var/www/techotopia/includes/diff/DiffFormatter.php(140): TableDiffFormatter->changed()
#8 /var/www/techotopia/includes/diff/DiffFormatter.php(82): DiffFormatter->block()
#9 /var/www/techotopia/includes/diff/DifferenceEngine.php(881): DiffFormatter->format()
#10 /var/www/techotopia/includes/diff/DifferenceEngine.php(797): DifferenceEngine->generateTextDiffBody()
#11 /var/www/techotopia/includes/diff/DifferenceEngine.php(728): DifferenceEngine->generateContentDiffBody()
#12 /var/www/techotopia/includes/diff/DifferenceEngine.php(662): DifferenceEngine->getDiffBody()
#13 /var/www/techotopia/includes/diff/DifferenceEngine.php(632): DifferenceEngine->getDiff()
#14 /var/www/techotopia/includes/diff/DifferenceEngine.php(453): DifferenceEngine->showDiff()
#15 /var/www/techotopia/includes/page/Article.php(797): DifferenceEngine->showDiffPage()
#16 /var/www/techotopia/includes/page/Article.php(508): Article->showDiffPage()
#17 /var/www/techotopia/includes/actions/ViewAction.php(44): Article->view()
#18 /var/www/techotopia/includes/MediaWiki.php(490): ViewAction->show()
#19 /var/www/techotopia/includes/MediaWiki.php(287): MediaWiki->performAction()
#20 /var/www/techotopia/includes/MediaWiki.php(714): MediaWiki->performRequest()
#21 /var/www/techotopia/includes/MediaWiki.php(508): MediaWiki->main()
#22 /var/www/techotopia/index.php(41): MediaWiki->run()
#23 {main}