Below are few methods I follow as my own standards. Anyone who is is involved in larger projects must shape a convention as a programming standard. This makes a team to be bound in a common code formatting styles. It is better if your codes look pretty nice, while just, they are runnable. Think the future extension possibilities of your applcation and go through some of the best best practices.
Embed your variables into double quotes. Sometimes, it is said that using single quotes is good for performace. But in practice, capsulating the variables within the double quotes have a nice formatting, and cleaner looking. You can write the immediate varibles, or array elements or object members into the string. For example look at the below codes carefully:
$sql="SELECT * FROM {$config->database_prefix}_users;";
$sql="SELECT * FROM {$config['database_prefix']}_users;";
$sql="SELECT * FROM {$prefix}_users;";
Poor writing would do like this:
$sql='SELECT * FROM '.$config->database_prefix.'_users;';
$sql='SELECT * FROM '.$config['database_prefix'].'_users;';
$sql='SELECT * FROM '.$prefix.'_users;';
This is untidy code. I do not like this way. What about you?
Always try to enquote an array index, with a single quote, if it is of string type.
For example:
$name = $row['name'];
But, still the poor way of writing the same could do as:
$name = $row[name];
In this case, the index term (name) is not well defined php variables. It is not good idea to use like this.
If you needed to pull out the different variables based on the index of an array, you can extract. For example, the below lines can be replaced by a single line of code in PHP:
$name = $row['name'];
$email = $row['email'];
$weight = $row['weight'];
$url = $row['url'];
Replacement code is:
extract($row);
This sets up all the variables in the $row. Each index becomes a usable variable, even if it had empty values in it.
Warning: It may override the already used variables, if they match with the index name. So, be careful.
For example, if you wrote once, $weight = float_val($row['weight']); before using extract, this value will be lost. For the protection,
I love them all, and have written a big class on array mapping to format the array data. By mapping an array, you are going to process each accessible element of an array with a common php function. For example, you may want all the POST data to be trimmed off, you can use array_map function as:
$_POST = array_map('trim', $_POST);
But, if $_POST contained the midex data types including arrays, the contained arrays will be ruined. So, mapping function must be intelligent enough to check these cases.
Array mapping saves a lot of time, and probably, the memory too.