If we are familiar with programming, we are certainly familiar with variables, how to declare and use it. In this discussion, we will discuss more about the variables, with specific scope of PHP as programming language and what is the secret behind the PHP variables.

Variables are containers used to hold information and it can be used later in our script. A variables has a name and information is stored in the variable. So, how to we will declare and create it ..?

In PHP, we can declare a variable begin with a $ (dollar) sign before the variable name. with the dollar sign helps we distinguish a variable from a function, a keyword or any other part of PHP. In general, when we declare a variable, we always assigned a value as default. Following script bellow for example, how to we declare and create a variable.

1
2
3
4
5
6
7
8
9
10
11
12
13
 
	<?php
		/* declare a variable */
		$myVariable;
 
		/* declare a variable and assigned a value as default */
		$stringVariable = “default_variable”;
 
		$integerValue = 234;
 
		$doubleValue = 234.456;
 
	?>

When we are naming a variable, keep the follwing rule in mind :

  1. All variable names have a dollar sign in front of them.
  2. Variable names can be any length.
  3. Variable names can include letters, numbers and underscores only.
  4. Variable names must begin with a letter or an underscores.
  5. Case sensitive, uppercase and lowercase letters are not the same.

There are change how variable behavior between PHPv3 ( and previous version ) and PHPv4 ( and later version ). In PHP v3, a variable are always assigned by value and when we assign an expression to a variable, the entire value of the original expression is copied into the destination variable. So, if we change one of those variable will have no effect on the other. In the PHP v4 and later version, there are fundamental changes of PHP variables behavior. In the PHP v4 another way to assign values to variable : assign by reference. When we assign a variable from other variable, the new variable simply references ( become an alias ) the original variable. So, if we change to the new variable affect the original and vice versa. We can assign by reference for other variable, add an ampersand ( & ) in the beginning of the variable which is being assigned ( the source variable ). See example bellow :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
	/* First Variable  */
	$firstVariable =Default Value”;
 
	/* Reference to other variable */
	$otherVariable = &$firstVariable;
 
	/* Change reference variable */
	$otherVariable = “Change variable”;
 
	/* firstVariable = otherVariable */
	echo $firstVariable;
	echo $otherVariable;
 
?>

In this example, we have demonstrated assign by reference. $firstVariable and $otherVariable share the same block of memory, we can say that $otherVariable as alias $firstVariable. With this case, PHP understands there are two references to that block of memory.

PHP as programming language, like any kind of others programming language is used to create application that it’s consume memory. PHP uses some memory for internal purposes and some memory stores the data that we work within in our application, such as variables. Typically, PHP application consume small amounts of memory, so we don’t have to worry about it and frees them automatically the ends of each request.

In PHP, we also can set a variable to NULL or unset it to remove a variable from memory. This function we need if we develop a large application that make use of many variable and consume larger chunks of memory. We also test a value of the variable using is_null, isset and empty functions.

See another discussion about of PHP language in this site, like basic learning PHP Function, basic learning PHP comment, basic learning PHP array and others.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • Slashdot
  • Technorati
  • YahooMyWeb