// PHPLANG — LANGUAGE REFERENCE

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

Docs GitHub
// Color scheme

>_LANGUAGE REFERENCE

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.

Keyword

# if

conditional branch; runs its block when the condition is truthy

if (1 < 2) echo "y";   // => y

# elseif

additional condition branch tested when the preceding ones fail

if (0) echo "a"; elseif (1) echo "b";   // => b

# else

fallback branch taken when every preceding condition is falsey

if (0) echo "a"; else echo "b";   // => b

# while

loop while the condition stays truthy (tested before each pass)

$i = 0; while ($i < 3) $i++; echo $i;   // => 3

# do

do/while loop; the body always runs at least once

$i = 5; do $i++; while ($i < 3); echo $i;   // => 6

# for

C-style loop with init, condition, and step clauses

$s = 0; for ($i = 1; $i <= 3; $i++) $s += $i; echo $s;   // => 6

# foreach

iterate an array as `$value` or `$key => $value`

foreach ([1, 2] as $v) echo $v;   // => 12

# as

binds the current element inside a `foreach` header

foreach ([9] as $k => $v) echo $k;   // => 0

# switch

multi-way branch; cases compare with loose (`==`) equality

switch (2) { case 2: echo "two"; }   // => two

# case

a labelled `switch` branch; falls through until a `break`

switch (1) { case 1: echo "a"; case 2: echo "b"; }   // => ab

# default

the `switch`/`match` branch taken when no case matches

switch (9) { default: echo "d"; }   // => d

# break

exit the nearest enclosing loop or `switch` immediately

foreach ([1, 2, 3] as $v) { if ($v == 2) break; echo $v; }   // => 1

# continue

skip to the next iteration of the nearest loop

foreach ([1, 2, 3] as $v) { if ($v == 2) continue; echo $v; }   // => 13

# function

define a named function; `return` yields its value (else null)

function sq($x) { return $x * $x; } echo sq(4);   // => 16

# return

return a value from the current function

function one() { return 1; } echo one();   // => 1

# match

PHP 8 expression; strict (`===`) compare of the subject to each arm

echo match (2) { 1 => "a", 2 => "b" };   // => b

# array

array literal constructor; `array(...)` is the same as `[...]`

echo count(array(1, 2, 3));   // => 3

# and

low-precedence logical AND (short-circuits)

echo (true and false) ? "y" : "n";   // => n

# or

low-precedence logical OR (short-circuits)

echo (false or true) ? "y" : "n";   // => y

# true

the boolean true literal

echo true ? "y" : "n";   // => y

# false

the boolean false literal

echo false ? "y" : "n";   // => n

# null

the null literal; the absent / unset value

echo null ?? "fallback";   // => fallback

# class

declare a class: properties, methods, constants, and `__construct`

class C { public $x = 1; } $o = new C; echo $o->x;   // => 1

# extends

single 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

# new

instantiate a class, passing constructor arguments

class C { function __construct($x) { $this->x = $x; } } echo (new C(5))->x;   // => 5

# fn

arrow function: single-expression body, auto-captures free variables

$d = fn($x) => $x * 2; echo $d(4);   // => 8

# use

capture enclosing variables (by value) into an anonymous `function`

$n = 10; $f = function () use ($n) { return $n; }; echo $f();   // => 10

# throw

raise an exception; usable as a statement and a PHP 8 expression

try { throw new Exception("boom"); } catch (Exception $e) { echo $e->getMessage(); }   // => boom

# try

guard a block; `catch` handles a thrown exception, `finally` always runs

try { echo "a"; } finally { echo "b"; }   // => ab

# catch

handle a matching exception type (`catch (A | B $e)`); binds the object

try { throw new Exception("x"); } catch (Exception $e) { echo $e->getMessage(); }   // => x

# finally

runs after `try`/`catch` on every exit — return, throw, break, continue

try { echo "t"; } finally { echo "f"; }   // => tf

Construct

# echo

write one or more values to stdout (no return value)

echo "hi", "!";   // => hi!

# print

write a single value to stdout; evaluates to 1

print "hi";   // => hi

# isset

true if every argument is set and not null (quiet on unset)

$a = 1; echo isset($a) ? "y" : "n";   // => y

# empty

true if the argument is falsey or unset (quiet on unset)

echo empty(0) ? "y" : "n";   // => y

Cast

# (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

String

# strlen

byte length of a string

echo strlen("abc");   // => 3

# strtoupper

copy with all letters uppercased

echo strtoupper("abc");   // => ABC

# strtolower

copy with all letters lowercased

echo strtolower("ABC");   // => abc

# ucfirst

copy with the first character uppercased

echo ucfirst("hello");   // => Hello

# lcfirst

copy with the first character lowercased

echo lcfirst("Hello");   // => hello

# ucwords

copy with the first letter of each word uppercased

echo ucwords("a b");   // => A B

# trim

copy with leading and trailing whitespace removed

echo trim("  hi  ");   // => hi

# ltrim

copy with leading whitespace removed

echo ltrim("  hi");   // => hi

# rtrim

copy with trailing whitespace removed (alias `chop`)

echo rtrim("hi  ") . "!";   // => hi!

# str_repeat

the string repeated n times

echo str_repeat("ab", 3);   // => ababab

# strrev

the string with its characters reversed

echo strrev("abc");   // => cba

# wordwrap

wrap a string to a column width at word boundaries

echo wordwrap("a b c", 3, "|", true);   // => a b|c

# substr

a substring from an offset (negative counts from the end)

echo substr("hello", 1, 3);   // => ell

# strpos

first index of a substring, or false if absent

echo strpos("hello", "l");   // => 2

# str_replace

copy with every occurrence of the search replaced

echo str_replace("a", "b", "aaa");   // => bbb

# implode

join array elements with a separator (alias `join`)

echo implode(",", [1, 2, 3]);   // => 1,2,3

# explode

split a string on a separator into an array

echo explode(",", "a,b")[1];   // => b

# str_split

split a string into an array of chunks (default length 1)

echo str_split("ab")[0];   // => a

# str_pad

pad a string to a length (right pad by default)

echo str_pad("5", 3, "0");   // => 500

# str_contains

true if the haystack contains the needle

echo str_contains("hello", "ell") ? "y" : "n";   // => y

# str_starts_with

true if the string starts with the prefix

echo str_starts_with("hello", "he") ? "y" : "n";   // => y

# str_ends_with

true if the string ends with the suffix

echo str_ends_with("hello", "lo") ? "y" : "n";   // => y

# str_word_count

number of whitespace-separated words in the string

echo str_word_count("a b c");   // => 3

# number_format

format a number with grouped thousands and fixed decimals

echo number_format(1234.567, 2);   // => 1,234.57

# htmlspecialchars

escape &, <, >, and quotes for HTML (alias `htmlentities`)

echo htmlspecialchars("a<b");   // => a&lt;b

# strcmp

byte comparison of two strings: <0, 0, or >0

echo strcmp("a", "b");   // => -1

# strcasecmp

case-insensitive comparison of two strings

echo strcasecmp("A", "a");   // => 0

# chr

one-character string for a byte value (mod 256)

echo chr(65);   // => A

# ord

byte value of the first character of a string

echo ord("A");   // => 65

# dechex

lowercase hexadecimal string of an integer

echo dechex(255);   // => ff

# hexdec

integer value of a hexadecimal string

echo hexdec("ff");   // => 255

# bin2hex

hex representation of each byte in a string

echo bin2hex("AB");   // => 4142

# sprintf

format arguments into a string using a printf format

echo sprintf("%d-%s", 1, "x");   // => 1-x

Array

# count

number of elements in an array (alias `sizeof`)

echo count([1, 2, 3]);   // => 3

# array_keys

a new array of the array's keys

echo implode(",", array_keys(["a" => 1, "b" => 2]));   // => a,b

# array_values

a new array of the array's values, reindexed from 0

echo implode(",", array_values([5 => "a", 9 => "b"]));   // => a,b

# array_push

append one or more elements to the end of an array

$a = [1]; array_push($a, 2); echo implode(",", $a);   // => 1,2

# in_array

true if a value is present in the array

echo in_array(2, [1, 2, 3]) ? "y" : "n";   // => y

# range

array of a numeric or character sequence

echo implode(",", range(1, 4));   // => 1,2,3,4

# array_merge

concatenate arrays, reindexing integer keys

echo implode(",", array_merge([1], [2, 3]));   // => 1,2,3

# array_map

apply a callback to each element, returning a new array

echo implode(",", array_map("strtoupper", ["a", "b"]));   // => A,B

# array_filter

keep elements for which the callback is truthy

echo implode(",", array_filter([0, 1, 2]));   // => 1,2

# array_reduce

fold an array to a single value with a callback

echo array_reduce([1, 2, 3], fn($c, $x) => $c + $x, 0);   // => 6

# array_slice

a slice of an array from an offset for a length

echo implode(",", array_slice([1, 2, 3, 4], 1, 2));   // => 2,3

# array_reverse

a new array with the elements in reverse order

echo implode(",", array_reverse([1, 2, 3]));   // => 3,2,1

# array_sum

sum of the array's numeric values

echo array_sum([1, 2, 3]);   // => 6

# array_product

product of the array's numeric values

echo array_product([2, 3, 4]);   // => 24

# array_flip

a new array with keys and values swapped

echo array_flip(["a", "b"])["a"];   // => 0

# array_unique

a new array with duplicate values removed

echo implode(",", array_unique([1, 1, 2]));   // => 1,2

# array_key_exists

true if the key exists (alias `key_exists`)

echo array_key_exists("a", ["a" => 1]) ? "y" : "n";   // => y

# array_search

the key of the first matching value, or false

echo array_search(2, [1, 2, 3]);   // => 1

# sort

sort an array in ascending order, reindexing keys

$a = [3, 1, 2]; sort($a); echo implode(",", $a);   // => 1,2,3

# rsort

sort an array in descending order, reindexing keys

$a = [1, 3, 2]; rsort($a); echo implode(",", $a);   // => 3,2,1

# asort

sort by value ascending, preserving key association

$a = ["b" => 2, "a" => 1]; asort($a); echo implode(",", array_keys($a));   // => a,b

# arsort

sort by value descending, preserving key association

$a = ["a" => 1, "b" => 2]; arsort($a); echo implode(",", array_keys($a));   // => b,a

# ksort

sort by key ascending

$a = ["b" => 1, "a" => 2]; ksort($a); echo implode(",", array_keys($a));   // => a,b

# krsort

sort by key descending

$a = ["a" => 1, "b" => 2]; krsort($a); echo implode(",", array_keys($a));   // => b,a

# array_fill

an array of count copies of a value from a start index

echo implode(",", array_fill(0, 3, "x"));   // => x,x,x

# array_combine

an array pairing one array's values as keys with another's

echo array_combine(["a"], [1])["a"];   // => 1

# array_diff

values in the first array absent from the others

echo implode(",", array_diff([1, 2, 3], [2]));   // => 1,3

# array_intersect

values present in every array

echo implode(",", array_intersect([1, 2, 3], [2, 3, 4]));   // => 2,3

Math

# abs

absolute value of a number

echo abs(-5);   // => 5

# floor

largest integer value not greater than x, as a float

echo floor(3.7);   // => 3

# ceil

smallest integer value not less than x, as a float

echo ceil(3.2);   // => 4

# round

round to an optional number of decimal places

echo round(3.14159, 2);   // => 3.14

# sqrt

non-negative square root, as a float

echo sqrt(16);   // => 4

# pow

base raised to an exponent

echo pow(2, 10);   // => 1024

# intdiv

integer division of two integers (truncated)

echo intdiv(7, 2);   // => 3

# fmod

floating-point remainder of x / y

echo fmod(7.5, 2);   // => 1.5

# sin

sine of x (radians)

echo sin(0);   // => 0

# cos

cosine of x (radians)

echo cos(0);   // => 1

# tan

tangent of x (radians)

echo tan(0);   // => 0

# exp

e raised to the power x

echo exp(0);   // => 1

# log

natural log, or log base b when a second arg is given

echo log(8, 2);   // => 3

# log10

base-10 logarithm of x

echo log10(1000);   // => 3

# pi

the constant pi as a float

echo round(pi(), 2);   // => 3.14

# max

largest of the arguments or of an array

echo max(3, 1, 2);   // => 3

# min

smallest of the arguments or of an array

echo min(3, 1, 2);   // => 1

Type

# gettype

the type name of a value

echo gettype(1);   // => integer

# is_array

true if the value is an array

echo is_array([1]) ? "y" : "n";   // => y

# is_int

true if the value is an integer (aliases is_integer/is_long)

echo is_int(5) ? "y" : "n";   // => y

# is_float

true if the value is a float (alias is_double)

echo is_float(1.5) ? "y" : "n";   // => y

# is_string

true if the value is a string

echo is_string("x") ? "y" : "n";   // => y

# is_bool

true if the value is a boolean

echo is_bool(true) ? "y" : "n";   // => y

# is_null

true if the value is null

echo is_null(null) ? "y" : "n";   // => y

# is_numeric

true for a number or a numeric string

echo is_numeric("3.5") ? "y" : "n";   // => y

# is_callable

true if the value names something callable

echo is_callable("strlen") ? "y" : "n";   // => y

# intval

the integer value of a variable

echo intval("42px");   // => 42

# floatval

the float value of a variable (alias doubleval)

echo floatval("3.5kg");   // => 3.5

# strval

the string value of a variable

echo strval(42);   // => 42

# boolval

the boolean value of a variable

echo boolval("") ? "y" : "n";   // => n

Output

# print_r

human-readable dump of a value; returns it when the 2nd arg is true

echo print_r([1, 2], true);   // => Array ( [0] => 1 [1] => 2 )

# printf

write a printf-formatted string; returns its byte length

printf("%d", 7);   // => 7

# var_dump

dump a value with its type and structure

var_dump(true);   // => bool(true)

# var_export

output (or return) a parsable PHP representation of a value

var_export(1);   // => 1

# json_encode

encode a value as a JSON string

echo json_encode([1, 2, 3]);   // => [1,2,3]

More