# PHP syntax for pilots

[Wiki home](index.md) · [Getting started](getting-started.md) · [PHP reference](php-reference.md)

A pilot is a standalone script. Logic Bots parses it, checks it against the supported PHP subset, and interprets its top-level statements once per tick for each living bot with a pilot. The engine injects `$pilot` for that tick. Leave `<?php` at the beginning or omit it. `declare(strict_types=1);` is the supported declaration. A top-level `return;` ends the current decision. The script starts again on the next tick if the bot is still alive and the match continues.

## What you can write

| Category | Supported forms |
| --- | --- |
| Values | Integers, finite floats, strings, booleans, `null`, and indexed or associative arrays |
| Variables | Ordinary locals, individual array entries, assignment, supported compound assignment, increments, and decrements |
| Decisions | `if`, `elseif`, `else`, ternary expressions, null coalescing, `isset`, and `empty` |
| Loops | `for`, `foreach` over arrays, and `while`. Use `break` and `continue` for the immediate enclosing loop. |
| Operators | Arithmetic, comparisons including `===` and `<=>`, Boolean logic, `.` for scalar string concatenation, and casts to `int`, `float`, `bool`, or `string` |
| Calls | Named helpers, allowlisted PHP functions, and documented methods called directly on `$pilot` |

Use literal strings and `.` to combine values. Classes, closures, arrow functions, references, destructuring, spread syntax, exceptions, namespaces, dynamic calls, and direct file or network functions are outside the pilot subset. Use `debug()` to record values in a practice trace. Pass facts from an observation to helpers and call `$pilot` directly where needed. The interpreter reserves `$pilot` as a method receiver.

## Helpers and local state

Define helpers at the top level. Every parameter and return type is required. Use `int`, `float`, `bool`, `string`, `array`, nullable forms such as `?array`, or `mixed`. Use `void` for a helper that finishes without returning a value. The interpreter checks arguments and results at runtime. An integer can widen to a float. Defaults can use literals, literal arrays, and supported constants. Position required parameters first. Named helper arguments can follow positional arguments.

Each helper call starts with its own locals. Ordinary variables reset on every tick. Use `remember()` and `recall()` for small facts that should survive to the next tick, such as a target ID or a cooldown deadline. Memory belongs to the individual bot for the lifetime of the match. The whole JSON-encoded map can use 4,096 bytes. Call `remember($key, null)` to clear a key. Scan again each tick because a remembered target can move, cloak, or be destroyed.

This pilot stores the nearest opponent ID only when it can see one within 36 units. Because `scan()['enemies']` is distance-ordered, the helper can stop after the first entry. In Test Run the `target-id` debug label shows the decision for each recorded tick.

```php
<?php
function nearbyEnemy(array $contacts, float $limit = 36.0): ?array
{
    $nearest = $contacts[0] ?? null;
    if ($nearest === null || $nearest['distance'] > $limit) {
        return null;
    }
    return $nearest;
}

$scan = $pilot->scan();
$target = nearbyEnemy($scan['enemies'], limit: 36.0);
$targetId = $target['id'] ?? null;
$pilot->remember('target-id', $targetId);
$pilot->debug('target-id', $targetId);
```

This snippet only observes and stores a decision. For a pilot that also drives and fires, see the [Pilot API](pilot-api.md) and [working examples](examples.md).

## Functions and constants

| Family | Typical use | Examples |
| --- | --- | --- |
| Math | Distance, orientation, clamping, and thresholds | `hypot`, `atan2`, `min`, `max`, `abs`, `round`, `sqrt` |
| Arrays | Read keys or columns and work with short lists | `count`, `in_array`, `array_key_exists`, `array_column`, `array_slice` |
| Types | Check optional values before using an observation | `is_array`, `is_null`, `is_int`, `is_numeric`, `is_finite` |
| Strings | Inspect or normalize small labels | `strlen`, `substr`, `strtolower`, `strtoupper` |

The supported constants are `true`, `false`, `null`, `M_PI`, `M_PI_2`, and `M_PI_4`. Use the [live PHP reference](php-reference.md) for the complete list and signatures. Use `$pilot->randomInt($min, $max)` for inclusive, seeded variation. Each pilot gets an independent stream.

## Budgets and errors

| Limit | Allowance |
| --- | --- |
| Source | 64,000 UTF-8 bytes, 8,000 lexer tokens, 2,000 syntax nodes, and 48 levels of syntax nesting |
| Calls | Up to 12 arguments per call or parameters per helper, and 16 active helper calls |
| Interpreter work | 10,000 work units per pilot tick, including statements, expressions, values, and some forecasts |
| Local data | Up to 128 locals per frame and a 65,536-byte measured budget shared with active helper frames |
| Individual values | 4,096 bytes per string, 256 entries per array, up to 24,576 bytes per measured value, and bounded nesting |

Ordinary matches also share a 35,000,000-unit match work budget across player-authored pilots. Training presets use the same per-tick interpreter but leave this match budget to player-authored code. In matches that forfeit invalid pilots, the engine divides the match work budget across participant seats. See [Architecture](architecture.md) for how each match type handles script failures.

A script can compile and still fail when a particular branch encounters a missing entry, division by zero, a non-finite number, or an invalid helper argument. Use `??` or `isset` for optional data. Read a scan once and reuse it. Pass only the contact or coordinates a helper needs and keep loops short. `workRemaining()` helps you decide whether to run another forecast. Compile first, then use [Testing](testing.md) to exercise the decisions in different situations. [Troubleshooting](troubleshooting.md) explains common diagnostics.
