Searching...
Thursday 13 June 2013

PHP Basics - Part 5 :: Variables Part 1 :: Declaration

Identifiers

Identifier is a general term applied to variables, functions, and various other user-defined objects. There are several properties that PHP identifiers must abide by:

  • An identifier can consist of one or more characters and must begin with a letter or an underscore. Furthermore, identifiers can consist of only letters, numbers, underscore characters, and other ASCII characters from 127 through 255.
  • Identifiers are case sensitive. Therefore, a variable named $recipe is different from a variable named $Recipe, $rEciPe, or $recipE.
  • Identifiers can be any length. This is advantageous because it enables a programmer to accurately describe the identifier’s purpose via the identifier name.
  • An identifier name can’t be identical to any of PHP’s predefined keywords. You can find a complete list of these keywords in the PHP manual appendix.(REFER TO PHP WEBSITE)


Variables

A variable is a symbol that can store different values at different times. For example, suppose you create a web-based calculator capable of performing mathematical tasks. Of course, the user will want to input values of his choosing; therefore, the program must be able to dynamically store those values and perform calculations accordingly. At the same time, the programmer requires a user-friendly means for referring to these value-holders within the application. The variable accomplishes both tasks.
You can think of variables as a memory block where a value is stored. Suppose initially that variable stores 5 {x=5}. Now suppose function demands: x =+ 5. Now the value stored in variable x is not 5 but is 10. Hence, the variable remains the same but it's value is changed to 10.

Hence we can say, A variable is a named memory location that contains data and may be manipulated throughout the execution of the program.

Variable Declaration

A variable always begins with a dollar sign, $, which is then followed by the variable name. Variable names follow the same naming rules as identifiers. That is, a variable name can begin with either a letter or an underscore and can consist of letters, underscores, numbers, or other ASCII characters ranging from 127 through 255. The following are all valid variables:

• $color
• $operating_system
• $_some_variable
• $model

Note that variables are case sensitive. For instance, the following variables bear no relation to one another:

• $color
• $Color
• $COLOR

Interestingly, variables do not have to be explicitly declared in PHP as they do in a language such as C. Rather, variables can be declared and assigned values simultaneously. Nonetheless, just because you can do something doesn’t mean you should. Good programming practice dictates that all variables should be declared prior to use, preferably with an accompanying comment. Once you’ve declared your variables, you can begin assigning values to them. Two methodologies are available for variable assignment: by value and by reference.

Value Assignment

Assignment by value simply involves copying the value of the assigned expression to the variable assignee. This is the most common type of assignment. A few examples follow:

$color = "red";
$number = 12;
$age = 12;
$sum = 12 + "15"; // $sum = 27

Keep in mind that each of these variables possesses a copy of the expression assigned to it. For example, $number and $age each possesses their own unique copy of the value 12. If you prefer that two variables point to the same copy of a value, you need to assign by reference.

Reference Assignment

PHP 4 introduced the ability to assign variables by reference, which essentially means that you can create a variable that refers to the same content as another variable does. Therefore, a change to any variable referencing a particular item of variable content will be reflected among all other variables referencing that same content. You can assign variables by reference by appending an ampersand (&) to the equal sign. Let’s consider an example:

<?php
$value1 = "Hello";
$value2 =& $value1; // $value1 and $value2 both equal "Hello"
$value2 = "Goodbye"; // $value1 and $value2 both equal "Goodbye"
?>

An alternative reference-assignment syntax is also supported, which involves appending the ampersand to the front of the variable being referenced. The following example adheres to this new syntax:

<?php
$value1 = "Hello";
$value2 = &$value1; // $value1 and $value2 both equal "Hello"
$value2 = "Goodbye"; // $value1 and $value2 both equal "Goodbye"
?>


0 comments:

Post a Comment

 
Back to top!