# if
conditional branch; runs its block when the condition is truthy
if (1 < 2) echo "y"; // => y
phplang v0.1.0 · PHP on fusevm · lex/parse → AST → fusevm bytecode → Cranelift JIT · no bespoke VM · the first compiled standalone PHP runtime · MIT · in active development
Every reserved keyword, language construct, type cast, and library function the current phplang build recognizes, grouped by keyword then construct then cast then library area. This page is generated from the language corpus (src/lsp.rs) by the gen-docs binary, so it stays in sync with what the runtime actually implements. Keywords/constructs mirror lexer.rs and parser.rs; each library function mirrors a real dispatch arm in src/builtins.rs.
ifconditional branch; runs its block when the condition is truthy
if (1 < 2) echo "y"; // => y
elseifadditional condition branch tested when the preceding ones fail
if (0) echo "a"; elseif (1) echo "b"; // => b
elsefallback branch taken when every preceding condition is falsey
if (0) echo "a"; else echo "b"; // => b
whileloop while the condition stays truthy (tested before each pass)
$i = 0; while ($i < 3) $i++; echo $i; // => 3
dodo/while loop; the body always runs at least once
$i = 5; do $i++; while ($i < 3); echo $i; // => 6
forC-style loop with init, condition, and step clauses
$s = 0; for ($i = 1; $i <= 3; $i++) $s += $i; echo $s; // => 6
foreachiterate an array as `$value` or `$key => $value`
foreach ([1, 2] as $v) echo $v; // => 12
asbinds the current element inside a `foreach` header
foreach ([9] as $k => $v) echo $k; // => 0
switchmulti-way branch; cases compare with loose (`==`) equality
switch (2) { case 2: echo "two"; } // => two
casea labelled `switch` branch; falls through until a `break`
switch (1) { case 1: echo "a"; case 2: echo "b"; } // => ab
defaultthe `switch`/`match` branch taken when no case matches
switch (9) { default: echo "d"; } // => d
breakexit the nearest enclosing loop or `switch` immediately
foreach ([1, 2, 3] as $v) { if ($v == 2) break; echo $v; } // => 1
continueskip to the next iteration of the nearest loop
foreach ([1, 2, 3] as $v) { if ($v == 2) continue; echo $v; } // => 13
functiondefine a named function; `return` yields its value (else null)
function sq($x) { return $x * $x; } echo sq(4); // => 16
returnreturn a value from the current function
function one() { return 1; } echo one(); // => 1
matchPHP 8 expression; strict (`===`) compare of the subject to each arm
echo match (2) { 1 => "a", 2 => "b" }; // => b
arrayarray literal constructor; `array(...)` is the same as `[...]`
echo count(array(1, 2, 3)); // => 3
andlow-precedence logical AND (short-circuits)
echo (true and false) ? "y" : "n"; // => n
orlow-precedence logical OR (short-circuits)
echo (false or true) ? "y" : "n"; // => y
truethe boolean true literal
echo true ? "y" : "n"; // => y
falsethe boolean false literal
echo false ? "y" : "n"; // => n
nullthe null literal; the absent / unset value
echo null ?? "fallback"; // => fallback
classdeclare a class: properties, methods, constants, and `__construct`
class C { public $x = 1; } $o = new C; echo $o->x; // => 1
extendssingle inheritance; a child inherits its parent's methods and properties
class A { function f() { return 1; } } class B extends A {} echo (new B)->f(); // => 1
newinstantiate a class, passing constructor arguments
class C { function __construct($x) { $this->x = $x; } } echo (new C(5))->x; // => 5
fnarrow function: single-expression body, auto-captures free variables
$d = fn($x) => $x * 2; echo $d(4); // => 8
usecapture enclosing variables (by value) into an anonymous `function`
$n = 10; $f = function () use ($n) { return $n; }; echo $f(); // => 10
throwraise an exception; usable as a statement and a PHP 8 expression
try { throw new Exception("boom"); } catch (Exception $e) { echo $e->getMessage(); } // => boom
tryguard a block; `catch` handles a thrown exception, `finally` always runs
try { echo "a"; } finally { echo "b"; } // => ab
catchhandle a matching exception type (`catch (A | B $e)`); binds the object
try { throw new Exception("x"); } catch (Exception $e) { echo $e->getMessage(); } // => x
finallyruns after `try`/`catch` on every exit — return, throw, break, continue
try { echo "t"; } finally { echo "f"; } // => tf
echowrite one or more values to stdout (no return value)
echo "hi", "!"; // => hi!
printwrite a single value to stdout; evaluates to 1
print "hi"; // => hi
issettrue if every argument is set and not null (quiet on unset)
$a = 1; echo isset($a) ? "y" : "n"; // => y
emptytrue if the argument is falsey or unset (quiet on unset)
echo empty(0) ? "y" : "n"; // => y
(int)cast to integer (desugars to intval)
echo (int) "42px"; // => 42
(float)cast to float (desugars to floatval); also (double)/(real)
echo (float) "3.5"; // => 3.5
(string)cast to string (desugars to strval)
echo (string) 123; // => 123
(bool)cast to boolean (desugars to boolval); also (boolean)
echo (bool) 0 ? "y" : "n"; // => n
strlenbyte length of a string
echo strlen("abc"); // => 3
strtouppercopy with all letters uppercased
echo strtoupper("abc"); // => ABC
strtolowercopy with all letters lowercased
echo strtolower("ABC"); // => abc
ucfirstcopy with the first character uppercased
echo ucfirst("hello"); // => Hello
lcfirstcopy with the first character lowercased
echo lcfirst("Hello"); // => hello
ucwordscopy with the first letter of each word uppercased
echo ucwords("a b"); // => A B
trimcopy with leading and trailing whitespace removed
echo trim(" hi "); // => hi
ltrimcopy with leading whitespace removed
echo ltrim(" hi"); // => hi
rtrimcopy with trailing whitespace removed (alias `chop`)
echo rtrim("hi ") . "!"; // => hi!
str_repeatthe string repeated n times
echo str_repeat("ab", 3); // => ababab
strrevthe string with its characters reversed
echo strrev("abc"); // => cba
wordwrapwrap a string to a column width at word boundaries
echo wordwrap("a b c", 3, "|", true); // => a b|c
substra substring from an offset (negative counts from the end)
echo substr("hello", 1, 3); // => ell
strposfirst index of a substring, or false if absent
echo strpos("hello", "l"); // => 2
str_replacecopy with every occurrence of the search replaced
echo str_replace("a", "b", "aaa"); // => bbb
implodejoin array elements with a separator (alias `join`)
echo implode(",", [1, 2, 3]); // => 1,2,3
explodesplit a string on a separator into an array
echo explode(",", "a,b")[1]; // => b
str_splitsplit a string into an array of chunks (default length 1)
echo str_split("ab")[0]; // => a
str_padpad a string to a length (right pad by default)
echo str_pad("5", 3, "0"); // => 500
str_containstrue if the haystack contains the needle
echo str_contains("hello", "ell") ? "y" : "n"; // => y
str_starts_withtrue if the string starts with the prefix
echo str_starts_with("hello", "he") ? "y" : "n"; // => y
str_ends_withtrue if the string ends with the suffix
echo str_ends_with("hello", "lo") ? "y" : "n"; // => y
str_word_countnumber of whitespace-separated words in the string
echo str_word_count("a b c"); // => 3
number_formatformat a number with grouped thousands and fixed decimals
echo number_format(1234.567, 2); // => 1,234.57
htmlspecialcharsescape &, <, >, and quotes for HTML (alias `htmlentities`)
echo htmlspecialchars("a<b"); // => a<b
strcmpbyte comparison of two strings: <0, 0, or >0
echo strcmp("a", "b"); // => -1
strcasecmpcase-insensitive comparison of two strings
echo strcasecmp("A", "a"); // => 0
chrone-character string for a byte value (mod 256)
echo chr(65); // => A
ordbyte value of the first character of a string
echo ord("A"); // => 65
dechexlowercase hexadecimal string of an integer
echo dechex(255); // => ff
hexdecinteger value of a hexadecimal string
echo hexdec("ff"); // => 255
bin2hexhex representation of each byte in a string
echo bin2hex("AB"); // => 4142
sprintfformat arguments into a string using a printf format
echo sprintf("%d-%s", 1, "x"); // => 1-x
countnumber of elements in an array (alias `sizeof`)
echo count([1, 2, 3]); // => 3
array_keysa new array of the array's keys
echo implode(",", array_keys(["a" => 1, "b" => 2])); // => a,b
array_valuesa new array of the array's values, reindexed from 0
echo implode(",", array_values([5 => "a", 9 => "b"])); // => a,b
array_pushappend one or more elements to the end of an array
$a = [1]; array_push($a, 2); echo implode(",", $a); // => 1,2
in_arraytrue if a value is present in the array
echo in_array(2, [1, 2, 3]) ? "y" : "n"; // => y
rangearray of a numeric or character sequence
echo implode(",", range(1, 4)); // => 1,2,3,4
array_mergeconcatenate arrays, reindexing integer keys
echo implode(",", array_merge([1], [2, 3])); // => 1,2,3
array_mapapply a callback to each element, returning a new array
echo implode(",", array_map("strtoupper", ["a", "b"])); // => A,B
array_filterkeep elements for which the callback is truthy
echo implode(",", array_filter([0, 1, 2])); // => 1,2
array_reducefold an array to a single value with a callback
echo array_reduce([1, 2, 3], fn($c, $x) => $c + $x, 0); // => 6
array_slicea slice of an array from an offset for a length
echo implode(",", array_slice([1, 2, 3, 4], 1, 2)); // => 2,3
array_reversea new array with the elements in reverse order
echo implode(",", array_reverse([1, 2, 3])); // => 3,2,1
array_sumsum of the array's numeric values
echo array_sum([1, 2, 3]); // => 6
array_productproduct of the array's numeric values
echo array_product([2, 3, 4]); // => 24
array_flipa new array with keys and values swapped
echo array_flip(["a", "b"])["a"]; // => 0
array_uniquea new array with duplicate values removed
echo implode(",", array_unique([1, 1, 2])); // => 1,2
array_key_existstrue if the key exists (alias `key_exists`)
echo array_key_exists("a", ["a" => 1]) ? "y" : "n"; // => y
array_searchthe key of the first matching value, or false
echo array_search(2, [1, 2, 3]); // => 1
sortsort an array in ascending order, reindexing keys
$a = [3, 1, 2]; sort($a); echo implode(",", $a); // => 1,2,3
rsortsort an array in descending order, reindexing keys
$a = [1, 3, 2]; rsort($a); echo implode(",", $a); // => 3,2,1
asortsort by value ascending, preserving key association
$a = ["b" => 2, "a" => 1]; asort($a); echo implode(",", array_keys($a)); // => a,b
arsortsort by value descending, preserving key association
$a = ["a" => 1, "b" => 2]; arsort($a); echo implode(",", array_keys($a)); // => b,a
ksortsort by key ascending
$a = ["b" => 1, "a" => 2]; ksort($a); echo implode(",", array_keys($a)); // => a,b
krsortsort by key descending
$a = ["a" => 1, "b" => 2]; krsort($a); echo implode(",", array_keys($a)); // => b,a
array_fillan array of count copies of a value from a start index
echo implode(",", array_fill(0, 3, "x")); // => x,x,x
array_combinean array pairing one array's values as keys with another's
echo array_combine(["a"], [1])["a"]; // => 1
array_diffvalues in the first array absent from the others
echo implode(",", array_diff([1, 2, 3], [2])); // => 1,3
array_intersectvalues present in every array
echo implode(",", array_intersect([1, 2, 3], [2, 3, 4])); // => 2,3
absabsolute value of a number
echo abs(-5); // => 5
floorlargest integer value not greater than x, as a float
echo floor(3.7); // => 3
ceilsmallest integer value not less than x, as a float
echo ceil(3.2); // => 4
roundround to an optional number of decimal places
echo round(3.14159, 2); // => 3.14
sqrtnon-negative square root, as a float
echo sqrt(16); // => 4
powbase raised to an exponent
echo pow(2, 10); // => 1024
intdivinteger division of two integers (truncated)
echo intdiv(7, 2); // => 3
fmodfloating-point remainder of x / y
echo fmod(7.5, 2); // => 1.5
sinsine of x (radians)
echo sin(0); // => 0
coscosine of x (radians)
echo cos(0); // => 1
tantangent of x (radians)
echo tan(0); // => 0
expe raised to the power x
echo exp(0); // => 1
lognatural log, or log base b when a second arg is given
echo log(8, 2); // => 3
log10base-10 logarithm of x
echo log10(1000); // => 3
pithe constant pi as a float
echo round(pi(), 2); // => 3.14
maxlargest of the arguments or of an array
echo max(3, 1, 2); // => 3
minsmallest of the arguments or of an array
echo min(3, 1, 2); // => 1
gettypethe type name of a value
echo gettype(1); // => integer
is_arraytrue if the value is an array
echo is_array([1]) ? "y" : "n"; // => y
is_inttrue if the value is an integer (aliases is_integer/is_long)
echo is_int(5) ? "y" : "n"; // => y
is_floattrue if the value is a float (alias is_double)
echo is_float(1.5) ? "y" : "n"; // => y
is_stringtrue if the value is a string
echo is_string("x") ? "y" : "n"; // => y
is_booltrue if the value is a boolean
echo is_bool(true) ? "y" : "n"; // => y
is_nulltrue if the value is null
echo is_null(null) ? "y" : "n"; // => y
is_numerictrue for a number or a numeric string
echo is_numeric("3.5") ? "y" : "n"; // => y
is_callabletrue if the value names something callable
echo is_callable("strlen") ? "y" : "n"; // => y
intvalthe integer value of a variable
echo intval("42px"); // => 42
floatvalthe float value of a variable (alias doubleval)
echo floatval("3.5kg"); // => 3.5
strvalthe string value of a variable
echo strval(42); // => 42
boolvalthe boolean value of a variable
echo boolval("") ? "y" : "n"; // => n
print_rhuman-readable dump of a value; returns it when the 2nd arg is true
echo print_r([1, 2], true); // => Array ( [0] => 1 [1] => 2 )
printfwrite a printf-formatted string; returns its byte length
printf("%d", 7); // => 7
var_dumpdump a value with its type and structure
var_dump(true); // => bool(true)
var_exportoutput (or return) a parsable PHP representation of a value
var_export(1); // => 1
json_encodeencode a value as a JSON string
echo json_encode([1, 2, 3]); // => [1,2,3]