PHPScript basics

PHPScript looks and behaves like PHP. If you know PHP you can skip most of this; if not, this page covers everything you need to write automations.

Variables

No declaration needed. Assign with =. Variable names start with $.

$name = "Acme Inc";
$count = 42;
$active = true;

The trigger record is always available as $record.

Types

Strings, numbers, booleans (true/false), null, and arrays. Values convert as you'd expect in comparisons and string concatenation.

$total = 10 + 5;            // 15
$label = "Items: " . $total; // "Items: 15"  (. joins strings)

Operators

+  -  *  /  %        math
.                    string concatenation
== != > < >= <=      comparison
&& || !              logical and / or / not

Conditionals

if ( $record["status"] == "won" ) {
    record_comment("deals", $record["_meta"]["id"], "Closed!");
} elseif ( $record["status"] == "lost" ) {
    sys_log("Lost deal");
} else {
    sys_log("Still open");
}

Loops

$rows = records_query("tasks", ["field" => "done", "op" => "=", "value" => "0"](/help/field-done-op-value-0));
foreach ( $rows as $row ) {
    sys_log("Open task: " . $row["title"]);
}

for, while, and do … while are also available, along with break and continue.

Arrays

$list = ["a", "b", "c"];
$list[] = "d";              // append
$person = ["name" => "Sam", "age" => 30];  // associative
sys_log($person["name"]);   // Sam

Useful built-ins: count(), implode(), explode(), in_array(), array_keys(), array_values(), array_merge(), array_column(), sort().

JSON

$obj = fromjson('{"a":1,"b":2}');  // parse JSON to an array
$json = tojson($obj);              // encode an array to JSON

(json_decode / json_encode work too.)

Strings

Standard PHP string functions are available: trim(), strlen(), substr(), strtolower(), strtoupper(), str_replace(), sprintf(), strpos(), number_format(), and the regex helpers preg_match() / preg_replace(). See also preg_match_gf() for a one-line "match and return a group" helper.

How records are shaped

A record is an associative array. Its fields are keyed by field id, and a special _meta key holds the record's id and title.

$record["name"]            // a field value, by field id
$record["_meta"]["id"]     // the record's id
$record["_meta"]["title"]  // the record's title

In the visual builder, tokens map to this shape:

  • {{record.name}}$record["name"]
  • {{record.id}}$record["_meta"]["id"]

Functions that take a record id (like record_update()) want the numeric id, so pass $record["_meta"]["id"].

Multi-value fields (multi-select, multi-user, multi-lookup) are arrays. To use one inside a string, flatten it with flow_text(); to test membership, use field_contains().

Errors

Throw to stop the flow with an error (recorded on the run log):

if ( ! $record["email"] ) {
    throw new Exception("Record has no email");
}

Catch errors you expect:

try {
    $resp = curl_get("https://api.example.com/thing");
} catch ( Exception $e ) {
    sys_log("Request failed: " . $e->getMessage());
}

Debugging

Use sys_log() liberally — each line shows in the flow's run log, which you can open from the automation's run history.

sys_log("status is " . $record["status"]);

Ready for the functions? Browse the PHPScript function reference.