php programming
php-variablesA variable in PHP is a program defined representation for a value which has a set scope or area of the program it can ‘live‘ and be recognized or used. PHP has some conventions for naming variables which must be kept in mind:
  • A variable name must begin with a dollar sign and can contain only alpha-numeric characters and/or an underscore.
  • Variables are case sensitive so the variable $myvar is considered a different variable than $MyVar to the PHP system.
  • A variable name should describe its use and make sense to the reader.
  • A variable (i.e. or any other PHP program entities), cannot be named the same as any of the PHP reserved words.
A variable does not need to be declared before adding a value to it because the PHP language is not strongly typed. Additionally, Since PHP is loosely typed, the variable type is not declared. The newly created variable will take the data type of the any value assigned it. The data and thus the implied type can also be changed at any time after creation by filling it with a different ‘typed‘ value. Initializing a variable in PHP is performed by naming it and filling it with data or null value: The variable will however take a value of false, empty string or empty array so it should always be initialized upon creation. PHP will allow the programmer to embed variables inside of strings and PHP will perform the string replacements for the programmer:
?PHP
// initialization
$myvar = 57;
$myarr[2] = 57;
// use
echo "myvar = $myvar";
echo "myvar = $myarr[2]";
// Using curly brackets
echo "myvar = {$myarr[2]}";
echo "myvar = ${myarr[2]}";
?>
It is perhaps best practice to disregard use of variables embedded in strings altogether so the program editor can highlight variable names properly and the programmer will then code fewer errors as well: <?PHP echo "myvar = ".$myarr[2]; ?> It is very important to insure a variable has been initialized to prevent any possible hard errors from occurring. Surround first time use with an if/then statement that checks the variables state:
<?PHP
if($myvar)
{
echo "myvar = $myvar";
}
if(isset($myvar))
{
echo "myvar = $myvar";
}
?>

PHP Variable Scope

The scope of a variable refers to all program areas the variable can be used and is fully recognized or, its context. If a variable has been defined at the top of a PHP page it is accessible to any code residing below it including classes, include files, and functions. If a variable has been defined in a class its scope is limited to the class unless it has been given the additional public attribute in which case it is accessible to any code which has access to the class functions. If it has been defined in a function it is limited to the scope of the function itself. The same rules apply to any include files except that a variable must be defined before it can be used so any include files prior to the variable definition include file have no access to the variable being defined.

PHP Predefined Variables

PHP has several predefined variables for cookies, sessions and other uses which programmers use all the time. All predefined variables are defined globally.

PHP Good Variable Comments

Commenting source code is perhaps one of the most important and overlooked tasks a good programmer will do. With good comments salted within the program another programmer can pick up the same program and make a modification or bug fix to it months and even years later. Couple that need with the fact the maintaining programmer may speak a different language or even reside in another country and the importance of documentation in and outside of program code becomes apparent. All good programmers comment their code. Here are some acceptable PHP comments:
<?PHP
/*
* Comment
*/
# This is a comment
//================
// This is a comment
//----------------------------
// comment
//%%%%%%%%%%%%%%%%%%%%%%%%
// This is a comment
//%%%%%%%%%%%%%%%%%%%%%%%%
$myVar = 'string 1'; // this is a comment
?>

Using PHP Variable Variables With A Real World Example

PHP variable variables are variables whose names can be programmatically set and accessed. The double dollar sign signifies a new variable derived from the first which is then used as a new variable. One example of their use would be where posting values to a database or service requires that all values be populated out of dozens of user choices. In this task the PHP variable variable can come into its own right:
<?PHP
// Fill some variables with the posted values received
if(!empty($item_1))
{
// item amount 1
$tot_amt_1 = ($duration * $amt_1);
$tot_amt = ($duration * $tot_amt_1);
}
// in for live testing only
// print $item_1;
// formatting the numerical data
$tot_amt_1 = number_format($tot_amt_1, 2);
// Walk an array of variables and taking only those values which
// are filled in. The variable variables allow us to 'build' our 
field names.
// Applying those values to the $p- structure which is later
// processed. Taking only perhaps sparsely filled values and 
inputting in sequencial order
$i = 0;
$b = "";
$c = 0;
$d = "";
for ($i=1;$i7;$i++)
{
$b = strval($i);
$total = 'tot_amt_' . $b;
$$total =${$total};
if($$total0)
{
$c++;
$d = strval($c);
$item_name = 'item_name_' . $d;
$item_name2 = 'item_name_' . $b;
$$item_name =${$item_name2};
$p-add_field($item_name, $$item_name);
$item_number = 'item_number_' . $d;
$item_number2 = 'item_number_' . $b;
$$item_number =${$item_number2};
$p-add_field($item_number, $$item_number);
$item_amt = 'amount_' . $d;
$tot_amt2 = 'tot_amt_' . $b;
$$tot_amt = ${$tot_amt2};
$p->add_field($item_amt, $$tot_amt);
}
}
?>
All code included in this article is available for free download. Using variables wisely is a necessary and powerful talent for any PHP programmer.Every programmer should strive to keep his or her source code in the most uniform and easy to understand manner as possible. This is not possible without well named and documented variables.