Thursday, September 4, 2008

Variables in PHP

Variables are identifiers to the memory location to store data. A PHP program can contain as many variables as you’d want.
Variable names in PHP begin with a dollar sign "$" and the values are assigned using the “=” operator.
Following the $ sign should be an alphabet or an underscore.
A variable name can contain any number of alphabets, numbers or underscores.
A PHP variable name cannot contain special characters like *, +, @, # etc.
In PHP we need not specify the variable type, as PHP takes the data type of the assigned value.

1. $1name is an invalid variable name as it starts with a number.
2. $_name or $name1 are, however, valid PHP variable names.
3.
$Name = "David";
$Age = 16;
In the above case, we assign values to the variable without mentioning their type. PHP automatically recognizes $Age as a numeric variable and $Name as a string.

No comments: