PHP Variables

Variables are temporary storage containers. In PHP, a variable can contain any type of data, such as, for example, strings, integers, floating-point numbers, objects and arrays. PHP is loosely typed, meaning that it will implicitly change the type of a variable as needed, depending on the operation being performed on its value. This contrasts with strongly typed languages, like C and Java,where variables can only contain one type of data throughout their existence.

PHP variables are identified by a dollar sign $, followed by an identifier name. Variables must be named using only letters (a-z, A-Z), numbers and the underscore character; their namesmust start with either a letter or an underscore, and are one of only two identifier types in PHP that are case-sensitive (the other is constants, discussed below). Here are a few examples:

  • $name = 'valid'; // Valid name
  • $_name = 'valid'; // Valid name
  • $1name = 'invalid'; // Invalid name, starts with a number

Variable Variables
In PHP, it is also possible to create so-called variable variables. That is a variable whose name is contained in another variable. For example:

$name = 'foo';
$$name = 'bar';
echo $foo;
// Displays 'bar'

As you can see, in this example we start by creating a variable that contains the string foo. Next, we use the special syntax $$name to indicate that we want the interpreter to use the contents of $name to reference a new variable—thus creating the new variable $foo, which is then printed out normally.

Because of the availability of variable variables, it is indeed possible to create variables whose names do not follow the constraints listed above. This is also possible by defining the name between braces:

$name = '123';
/* 123 is your variable name, this would normally be invalid. */
$$name = '456';
// Again, you assign a value
echo ${'123'};
// Finally, using curly braces you can output ’456’

A technique similar to variable variables can also be used to hold function names inside a variable:

function myFunc(){
   echo 'myFunc!';
$f = 'myFunc';
$f(); // will call myFunc();

Clearly, this technique should be used with as much care as variable variables, as the opportunities for mistakes and security issues it raises are quite significant.

Determining If a Variable Exists
One of the downsides of the way PHP handles variables is that there is no way to ensure that any one of them will exist at any given point in the execution of a script. This can introduce a range of problems—from annoying warnings if you try output the value of a non-existent variable to significant security and functionality issues when variables are unexpectedly unavailable when you need them.

To mitigate this problem, you can use the special construct isset():
echo isset ($x);
A call to isset() will return true if a variable exists and has a value other than NULL.

Next Topic | Previous Topic

Post to Twitter Post to Digg Post to Facebook Post to Google Buzz Send Gmail

Leave a Comment

Your email address will not be published. Required fields are marked *