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.
if ($score >= 90) {
echo "A";
} elseif ($score >= 70) {
echo "B";
} else {
echo "C";
}
$i = 0;
while ($i < 3) {
echo $i;
$i++;
} // 012
for ($i = 1; $i <= 5; $i++) {
echo $i . " ";
} // 1 2 3 4 5
Two forms — value only, or key and value:
foreach ($list as $item) {
echo $item;
}
foreach ($map as $key => $val) {
echo $key . "=" . $val . " ";
}
while (1) {
$i++;
if ($i == 3) { continue; } // skip 3
if ($i > 5) { break; } // stop after 5
echo $i;
} // 1245
| Operator | Meaning | Example |
|---|---|---|
+ - * / % | Integer arithmetic | 7 % 3 → 1 |
. | 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++ |
0, the empty string, the string
"0", and the empty array are falsy. Everything else is truthy.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
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.
define("MAX", 100);
define("SITE", "rowan.id.au");
echo MAX; // 100 (bare name, no $)
echo defined("MAX"); // 1
included file, keep secrets and settings in a file outside the served directory
and include it. See the web chapter.| Function | Returns | Notes |
|---|---|---|
strlen($s) | int | Length of the string form. |
count($a) | int | Number of array elements (0 for non-arrays). |
int($x) | int | Coerce to integer. Handy for numeric form fields. |
str($x) | string | Coerce to string. |
isset($x) | int | 1 if the value is present/non-zero, else 0. |
defined("K") | int | 1 if the constant exists. |
define("K", v) | 1 | Define a constant. |
htmlspecialchars($s) | string | Escapes & < > " ' for safe HTML output. |
read($path) | string | Read a file's contents (empty string if missing). |
write($path, $data) | int | Write a file. 1 on success, 0 on failure. |
| Function | Returns | Notes |
|---|---|---|
qq_query($db, $sql) | array / int | SELECT → array of row-arrays; write → affected count. |
qq_exec($db, $sql) | int | Alias of qq_query for writes. |
qq_error() | string | Last SQL error message (empty if none). |
qq_quote($s) | string | SQL-quote a value — injection defense. |
Full details and patterns on the SQL chapter.
| Tag | Meaning |
|---|---|
<? … ?> | 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.