Pequod / PiROS manual ↑ Manual home
02 · Command reference

Language reference

Every control structure, operator, array form, and built-in function in Pequod — with exact syntax. This is the lookup page; the other chapters show them in context.

Control structures

if / elseif / else

if ($score >= 90) {
    echo "A";
} elseif ($score >= 70) {
    echo "B";
} else {
    echo "C";
}

while

$i = 0;
while ($i < 3) {
    echo $i;
    $i++;
}                          // 012

for

for ($i = 1; $i <= 5; $i++) {
    echo $i . " ";
}                          // 1 2 3 4 5

foreach

Two forms — value only, or key and value:

foreach ($list as $item) {
    echo $item;
}

foreach ($map as $key => $val) {
    echo $key . "=" . $val . " ";
}

break / continue

while (1) {
    $i++;
    if ($i == 3) { continue; }   // skip 3
    if ($i > 5)  { break; }      // stop after 5
    echo $i;
}                          // 1245

Operators

OperatorMeaningExample
+ - * / %Integer arithmetic7 % 31
.String concatenation"a" . "b"ab
== !=Equal / not equal$x == 5
< > <= >=Ordering$n >= 10
&& ||Logical, short-circuit$a && $b
!Logical not!$done
- (unary)Negation-$n
?:Ternary$ok ? "y" : "n"
= += -= *= /= .=Assignment$s .= "x"
++ --Increment / decrement$i++
Truthiness. 0, the empty string, the string "0", and the empty array are falsy. Everything else is truthy.

Arrays

Arrays are ordered and associative (insertion order is preserved).

// indexed
$a = [10, 20, 30];
echo $a[0];              // 10

// associative
$user = ["name" => "bob", "age" => 30];
echo $user["name"];      // bob

// append
$a[] = 40;               // $a is now [10,20,30,40]

// assign a key
$user["city"] = "SYD";

// size
echo count($a);         // 4

Functions

function greet($name, $times) {
    $out = "";
    for ($i = 0; $i < $times; $i++) {
        $out .= "hi $name! ";
    }
    return $out;
}
echo greet("ada", 2);    // hi ada! hi ada!

Functions have their own local scope; arguments are passed by value. A missing argument is 0. See Shell scripting for libraries via include.

Constants

define("MAX", 100);
define("SITE", "rowan.id.au");
echo MAX;                 // 100  (bare name, no $)
echo defined("MAX");     // 1
Config off the web root. Because constants can be set in an included file, keep secrets and settings in a file outside the served directory and include it. See the web chapter.

Built-in functions

FunctionReturnsNotes
strlen($s)intLength of the string form.
count($a)intNumber of array elements (0 for non-arrays).
int($x)intCoerce to integer. Handy for numeric form fields.
str($x)stringCoerce to string.
isset($x)int1 if the value is present/non-zero, else 0.
defined("K")int1 if the constant exists.
define("K", v)1Define a constant.
htmlspecialchars($s)stringEscapes & < > " ' for safe HTML output.
read($path)stringRead a file's contents (empty string if missing).
write($path, $data)intWrite a file. 1 on success, 0 on failure.

SQL built-ins (Queequeg)

FunctionReturnsNotes
qq_query($db, $sql)array / intSELECT → array of row-arrays; write → affected count.
qq_exec($db, $sql)intAlias of qq_query for writes.
qq_error()stringLast SQL error message (empty if none).
qq_quote($s)stringSQL-quote a value — injection defense.

Full details and patterns on the SQL chapter.

Template tags

TagMeaning
<? … ?>Run code.
<?= expr ?>Echo an expression (shorthand).
<?php … ?>Same as <? … ?> (accepted for familiarity).

Everything outside tags is emitted verbatim. Covered in depth in the web chapter.

← Basic usage Shell scripting →