Hopefully it will be a quick-start guide for BASIC programmers.
It also shows how to call functions both by reference and by value, like "byref" and "byval" calls in BASIC.
Single line remarks in php are denoted with a double slash, "//", the same as "REM" or the apostrophe, "'" in BASIC. Unlike BASIC, php uses "/*" and "*/" to denote multi-line remarks. Its self-explanatory, see the program remarks for an example.
Unlike BASIC, you do not need to redirect the program flow around functions. In the script below, the "die" directive is not really needed. In a purely testing environment it could be useful. It directs the browser to stop all following page execution, including HTML.
Notice that quotes can be either single or double. Single quotes are similar to BASIC. It is what it says. Double quotes instruct the compiler to parse the quoted material for other variables, expand them, and add them to the string. You may also use the period to denote that, saving a little parsing time. The time you would actually save is generally miniscule, and is getting less relevant with time, so do it as you like. These are functionally identical for variable "$b":
The output would be:<?php $a = 'red'; $b = "$a is a color"; //slowest, parses string (still very fast though) echo $b; $b = $a.' is a color'; //slightly faster, no parsing, reads 2 memory buffers echo $b; $b = 'red is a color'; //fastest, no parsing, reads one less memory buffer echo $b; ?>
red is a color red is a color red is a color
EXAMPLE:
<?php
/*
First we create the arrays. Note that the empty brackets designate an
auto-increment; the same as "i++" or "i=i+1"; and it starts at zero.
To create multidimensional arrays, just add another set of brackets.
However, remember to add the empty brackets.
Unlike BASIC, php can refer to array values by numbers, strings or a
combination thereof. If you don't add the empty brackets, the array
will not create a new dimension, it will simply add the new value to
the array.
In our array, if we did not add the empty brackets after "Apples", then
"Groff Special" would have overwritten "Washington" and the "key" would
have been "Apples".
Array elements are called "values". All array "values" have an index,
or "key". For this array:
array[] = "A";
array[] = "B";
array[] = "C";
The "keys" and "values" would be be as follows:
array[0] = "A";
array[1] = "B";
array[2] = "C";
Key 0 equals Value "A"
Key 1 equals Value "B"
Key 2 equals Value "C"
The same as in BASIC.
For this array:
array['1'] = "A";
array['letter'] = "B";
array[] = "C";
The "keys" and "values" would be be as follows:
array[0] = "C";
array['1'] = "A";
array['letter'] = "B";
Key 0 equals Value "A"
Key 1 equals Value "B"
Key 2 equals Value "C"
Its similar to using CONSTANTs in BASIC.
One difference between php and BASIC is that arrays can contain
other arrays. This may not be intuitive to a BASIC programmer.
For example, the "Apples" and "Walnuts" arrays are on the same
array level, in the same dimension, as the "Apples", "Oranges",
"Plums", and "Pears" entries.
For a little added insight, look at the output from this php
script.
*/
//create array
$key1 = 'Fruits';
$key2 = 'Nuts';
$testarray[$key1][] = "Apples";
$testarray[$key1][] = "Oranges";
$testarray[$key1][] = "Plums";
$testarray[$key1][] = "Pears";
$testarray[$key2][] = "Walnuts";
$testarray[$key2][] = "Almonds";
$testarray[$key2][] = "Pecans";
$testarray[$key2][] = "Filberts";
$testarray[$key1]['Apples'][] = "Washington";
$testarray[$key1]['Apples'][] = "Groff Special";
$testarray[$key2]['Walnuts'][] = "English";
$testarray[$key2]['Walnuts'][] = "Black";
//This is the function call
printarray($testarray);
die ('<hr>stop'); //don't execute the lines below
/* ------------------------------------------------------------
FUNCTIONS
*/ ------------------------------------------------------------
// Placing an ampersand before the variable, tells this function to access
// the array directly instead of making a copy. This is needed to get its
// actual name from the vname function.
function printarray(&$array) {
echo "<pre>";
if (is_array($array)) { // If it's an array, show formatted key-val pairs
reset($array);
walkarray($array, vname($array), '');
}
echo "</pre>";
}
function walkarray($array, $me='', $indent='') {
$startmsg = "<b>====</b> Start of '<b>$me</b>' Array <b>====</b>";
$l = strlen($startmsg);
echo $indent.$startmsg.'<br>';
while (list($key, $val) = each($array)) {
echo $indent."key = '<b>$key</b>' => val = '<b>$val</b>'<br>";
if (is_array($val)) { // If it's an array, recurse, iterate, and show it
walkarray($val, $key, $indent.' ');
}
}
echo $indent."<b>====</b> End of '<b>$me</b>' Array <b>====</b><br>";
}
//This function is from http://www.php.net/manual/en/language.variables.php#49997
function vname(&$var, $scope=false, $prefix='unique', $suffix='value') {
if($scope) $vals = $scope;
else $vals = $GLOBALS;
$old = $var;
$var = $new = $prefix.rand().$suffix;
$vname = FALSE;
foreach($vals as $key => $val) {
if($val === $new) $vname = $key;
}
$var = $old;
return $vname;
}
?>
Here is the output:
==== Start of 'testarray' Array ====
key = 'Fruits' => val = 'Array'
==== Start of 'Fruits' Array ====
key = '0' => val = 'Apples'
key = '1' => val = 'Oranges'
key = '2' => val = 'Plums'
key = '3' => val = 'Pears'
key = 'Apples' => val = 'Array'
==== Start of 'Apples' Array ====
key = '0' => val = 'Washington'
key = '1' => val = 'Groff Special'
==== End of 'Apples' Array ====
==== End of 'Fruits' Array ====
key = 'Nuts' => val = 'Array'
==== Start of 'Nuts' Array ====
key = '0' => val = 'Walnuts'
key = '1' => val = 'Almonds'
key = '2' => val = 'Pecans'
key = '3' => val = 'Filberts'
key = 'Walnuts' => val = 'Array'
==== Start of 'Walnuts' Array ====
key = '0' => val = 'English'
key = '1' => val = 'Black'
==== End of 'Walnuts' Array ====
==== End of 'Nuts' Array ====
==== End of 'testarray' Array ====