[Top]
   
Look at also other topic groups, which I have done!
You can return to this topic group by using this menu and the link Table of topic groups in the top of the page.
 
 
Search:
[Help]

Effective usage of variables

Topics

The basic idea of using variables and naming conventions of variables

The basic idea of PHP-variable is that they can be defined elsewhere than the place, where they have been used. In the example in the previous section I have defined them in the same page, which resemble the usage of the STYLE element.

If variables has been defined totally outside the master pages, the usage resemble using the @import at-rule in CSS, where the imported file is as if written to the current page.

Because both ordinary variable and and arrays have been needed one of my e-mail friend proposed that I should use following naming conventions, when in future could be at once see, what it the type of the variable:

Because string variable is the most used variable type, I don't set for ordinary string variables any prefix. Use mixed case (humorous "camel case") characters (for example $aMyArray), underscores (for example $a_my_array) or combination of previous mentioned naming types (for example $a_My_Array) in order to get names more readable.

[Top]

Repeated strings

All fragments can be written as separate files but small fragments are reasonable to put as variables. If pages has been alphabetic listed, but the order of pages might change or more pages might be added in future, it is reasonable to set the characters of the pages as string variables. When the order has been changed, it is necessary to edit much fewer pages. Variables can be used several times in a page. For example in the page, which handle dynamic menus I have used the same variable four times:

Link lists

In one of my previous example I handled pagelevel variables. Also link lists are reasonable to put as variable. Links can be sorted beforehand. By using sort() (from first to last) and asort() (from last to first) in order to sort links alphabetically the sorting base on the string of variables not on the name of the variable.

In order to get the desired result I have set for links class-attributes. For example

<?php
$CodeStyle ="<a class=\"CodeStyle\" href=\"http://www.codestyle.org/\" target=\"new\">CodeStyle</a>";
...
$aLinks = array($..., $..., $CodeStyle, $...);
sort($aLinks); /* The link http://www.codestyle.org becomes now first. */
?>
can be called ready sorted in other connection, where PHP generates the final link list, for example:
<ul>
<?php
include(../tutorials.inc';

for ($i = 0; $i < count($aLinks); $i++) /* Counting of the loop starts from zero. In each round the value increases with one until the counter finds that the loop has been reached the last variable. It is possible to use for arrays also the foreach function, which is a predefined array looping function. It converts arrays to string variables (for example foreach($aLinks as $someVariable)), when it is not necessary to use a counter. */

{
print ("<li>".$aLinks[$i]."</li>\n"); /* If we had used the foreach function the name of the variable would be the given new name, for example $someVariable. In this case it is necessary to use double quotation marks. If you use "..." you can insert variables into the string and have the output be the value of the variable. If you would use '...' the exact characters in the string will be printed out, not the value of the variable. It is not necessary to use the concatenation operator in the format ("<li>$aLinks[$i]</li>\n"). */
}
?>

</ul>

In principle it would logically be more reasonable to use the attribute id but the problem is that in the same page should never be the same value of the id attribute more than one. There might be situations, where the same link has been used several times in a page.

If I would not have used the class-attribute generating alphabetic order would have needed defining special keys. I have however tried to keep the system as simple as possible. Below is however a modified example on the base of a model from one of my e-mail friend:

$aLinks = array(
"CodeStyle" => "<a href=\"http://www.codestyle.org/\" target=\"new\">CodeStyle</a>",
...
);

At this way can create link files, which resemble link databases and which are easy to edit. In the example above organizing list and the final generating resemble data base managements but link-related individual variables must manually list inside the used array.

If link-related variables has directly been defined inside an array at the way below it is possible to get the whole array or an individual member of the array. Updating the array and adding more link-variables can be done together and updating the array doesn't cause any additional work.

<?php
$aLinks = array(
($CodeStyle ="<a class=\"CodeStyle\" href=\"http://www.codestyle.org/\" target=\"new\">CodeStyle</a>"),
($IndexDOTCss="<a class=\"IndexDOTCss\" href=\"http://www.blooberry.com/indexdot/css/\" target=\"new\">Index DOT Css</a>"),
...
);
?>

If it is necessary to create a new array, which has more individual variables than some existing array, new variables can be added to an existing arrays, which become a part of the new array. Below is an example of a new array, which have been create by using the array_push() function. The new array has one variable more than the previous created array.

<?php
$aLinksFi = $aLinks;
array_push($aLinksFi,
($KeijoKortelainen="<a class=\"KeijoKort\" href=\"http://www.2kmediat.com/\" target=\"new\">Keijo Kortelainen, 2K mediat</a>")); /* The used function add a new variable into the end of the existing array. */
?>

In my case the same result could have get by using the function array_unshift(), which put new variable in the beginning of the array. With the function array_merge() can be combined two or more arrays. In this example case the variable $KeijoKortelainen should have first set inside a new array.

Defining variables inside arrays has one disadvantage. If variables, which are inside arrays have been redefined, redefinitions don't concern arrays.

In order to get new values for variables, which are inside arrays, original variables inside arrays must remove and replace with changed variables by using the array_splice() function, for example:

$TopStyle=$TopStyle." (".$BradSoft.")";
$PageBuilder=$PageBuilder." (".$TafWS.")";
$DHTMLMenuB=$DHTMLMenuB." (".$xFX.")"; /* In the original $aApplications array previous listed variables have different values. */

array_splice($aApplications,2,3,array($TopStyle,$PageBuilder,$DHTMLMenuB));
/* Parameters 2,3 remove original variables. */

The function array_splice() remove the elements designated by offset and length from the input array, and replaces them with the elements of the replacement array, if supplied. It is not possible to remove variable by the names of variables. That's why it is always necessary to check offsets of removable variables and removable variables should be after each other in order to remove them with one command. That is in my mind the biggest disadvantage.

By using real databases links can be get by using names of fields. Because values have not been written directly inside arrays, local changes concern also arrays. Using the previous mentioned system adding new links into a link list is however approximately as easy as by using real databases. Below is an example of using databases, which should get the same result as one previous handled example getting data from an included file:

<ul>
<?php
$result = mysql($conn, "SELECT class, address, name FROM tutorials"); /* Lets presume that the used database table has fields id, class, address name and description, which in this example has been fetched information from three fields. */
while (list($class, $address, $name) = mysql_fetch_row($result)) {
print("<li><a class=\"$class\" href=\"$address\" target=\"new\">$name</a></li>");
}
?>

</ul>
[Top]