The Blog

The scripts that run off the sd card perform all tasks necessary for the application to operate, such as data logging, device control, ui layout and access control. The scripts are interpreted and firmware functions are called to carry out the actual function implementation. uPHP functions take variables as parameters and optionally return a variable. This tutorial is not meant as a primer in programming so much as a quick introduction for existing PHP coders into the internals of the language.
All variables start with $ symbol. Variables are automatically type cast by default. uPHP variables can be of the following types: integer, float, string and array. Assigning a variable a value will determine what type the variable takes. For example:

$x = 1; // this will create an int variable $x with a value of 1
$y = 1.1; // this will create a float variable $y with a value of 1.1
$z = "1"; // this will create a string $z with the value of 1

Mathematical operations can be performed on these variables, and you can type cast any variable using intval, floatval and strval.

$total = floatval($x)+ $y; // $total is now a float of value 2.1
$inttotal = intval($y) + $x; // $inttotal is now 2

You can use expressions with various parts just as in PHP:

$x = (sin($y) / cos($y)) * (sqrt($z / 8.7));

Arrays

Working with arrays is simple. First, if an array doesn't exist, create it like this:

$arr = array(); // blank array
$arr[0] = 1;
$arr[1] = 9;
$arr[2] = 3;

Or

$arr2 = array(1,9,3); // array with values in one go

One difference between PHP is that you cannot use => to assign name/value pairs. Instead, access the values by index.

For hashes, just use whatever string you want as a key:

$arr["name"] = "tom";
$arr["age"] = 76;

Use the sizeof function to get the number of elements, and the array_keys function to retrieve an array of the keys. To example, to print out GET parameters, you could do this:

$keys = array_keys($_GET);
for ($i = 0: $i < sizeof($_GET); $i++) {
	print("key=" . $keys[$i] . " and val = " . $_GET[$keys[$i]]. "<br>");
}

The print function will pass the output to the web page. Note the . concatenation for string values. Although PHP does not support the + operator on strings, uPHP would accept it.

Loops

uPHP handles for and while loops. For loops have the following syntax in pseudo code:

for (start condition; end condition; increment) {
 // your loop code
}

In a real example, the example show above in the array section illustrates how to loop through an array.
A while loop has the following syntax:

while (condition) {
  //your loop code
}

For example:

$x = 0;
while ($x < 10) { 
  print($x);
  $x++;
}

To be continued...

Comments | Add yours
  • No comments found
Add comment