PHP/Variables: Difference between revisions

wikademia>Baltazart
wikademia>Wikademia
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
Variables, as mentioned briefly in [[Introduction to PHP]], are a significant feature of any programming language. They can be used to store all forms of data, and are generally required to perform multiple manipulations on data not hard-coded into a script. This lesson will introduce the basics of variables in PHP.
Variables, as mentioned briefly in [[Introduction to PHP]], are a significant feature of any programming language. They can be used to store all forms of data, and are generally required to perform multiple manipulations on data not hard-coded into a script. This lesson will introduce the basics of variables in PHP.
 
{{ntnes}}
== Basic syntax ==
== Basic syntax ==
In PHP, all variables can be identified by the $ at the beginning of their name. Variables name generally start with a lowercase letter, however this can vary for special variables, which will be explained later. To understand how variables work, take this example:
In PHP, all variables can be identified by the $ at the beginning of their name. Variables name generally start with a lowercase letter, however this can vary for special variables, which will be explained later. To understand how variables work, take this example:
Line 51: Line 51:


== Declaration ==
== Declaration ==
In a more lower-level language like [[Topic:C++|C++]], the declaration of the type of each variable is needed. For example, the following code in PHP:
In a more lower-level language like [[C++|C++]], the declaration of the type of each variable is needed. For example, the following code in PHP:
<source lang="php">
<source lang="php">
<?php
<?php
Line 58: Line 58:
</source>
</source>


Does not explicitly define the type of variable ''a''. Of course, the PHP runtime environment recognizes during execution of this line that the programmer provided a string literal as the value for the variable. If you want to do the same in [[Topic:C++|C++]], you would would have to explicitly declare the variable type:
Does not explicitly define the type of variable ''a''. Of course, the PHP runtime environment recognizes during execution of this line that the programmer provided a string literal as the value for the variable. If you want to do the same in [[C++|C++]], you would would have to explicitly declare the variable type:
<source lang="php">
<source lang="php">
char[6] a;
char[6] a;
Line 88: Line 88:
</source>
</source>


The page 'another_page.php' has full acess to the variable 'var'.
The page 'another_page.php' has full access to the variable 'var'.
<source lang="php">
<source lang="php">
<?php
<?php
Line 104: Line 104:
===Globals===
===Globals===


You can force functions to acess other variables by using the global keyword, e.g:
You can force functions to access other variables by using the global keyword, e.g:
<source lang="php">
<source lang="php">
<?php
<?php