YOUSEFUL / FILEMAKER

Youseful PHP: FAQ, examples and tutorial

Learning center · Function reference

Use YsflPHP.fmx64 with an installed PHP command-line runtime. The tutorial uses process mode and PHP's JSON functions; it needs no web server or PHP framework.

FAQ

Do I need IIS, Apache or a PHP website? No. The plug-in runs php.exe directly. Use YSFL_PHP or the php=<path> option if discovery does not find your runtime. The host script uses PHP 8-era language/library features; use a compatible PHP 8 runtime.

Is there an embedded mode? This implementation supports process and worker. It rejects mode=inproc. Process is the default and starts fresh each call.

Should my code use return or echo? Use return for the value you want back. If nothing is returned, echoed text is used. Arrays become JSON. Output retrieves captured output; Error retrieves the detailed exception/trace.

Can a function defined in one call be used later? Use worker mode for that workflow. In process mode the first interpreter exits before the next call. Stop closes the worker; Reset clears user globals, not already-defined PHP functions.

Examples

Call a built-in function with a JSON argument array:

filemaker
YsflPh_Call ( "strtoupper" ; "[\"Youseful\"]" ; "mode=process timeout=15" )

Result: YOUSEFUL, provided PHP is found.

Return a PHP array as JSON:

filemaker
YsflPh_Run ( "return ['brand' => 'Youseful', 'count' => 3];" ; "mode=process timeout=15" )

Result: {"brand":"Youseful","count":3}.

Tutorial

Standalone sample scripts: PHP totals.

Goal: read JSON passed from FileMaker and return a summary.

  1. Confirm that the first example finds php.exe. If necessary, put php=<actual path to php.exe> on a separate option line before param=.
  2. Create Youseful - PHP totals:
filemaker
Set Variable [ $rows ; Value: "[{\"qty\":3,\"price\":10},{\"qty\":1,\"price\":7}]" ]
Set Variable [ $code ; Value:
    "$rows = json_decode($fm_param, true, 512, JSON_THROW_ON_ERROR); " &
    "$total = array_sum(array_map(fn($row) => $row['qty'] * $row['price'], $rows)); " &
    "return ['count' => count($rows), 'total' => $total];"
]
Set Variable [ $json ; Value: YsflPh_Run ( $code ; "mode=process timeout=15¶param=" & $rows ) ]
Set Variable [ $error ; Value: YsflPh_LastError ]
If [ $error <> 0 ]
    Set Variable [ $detail ; Value: YsflPh_Error ]
    Show Custom Dialog [ "PHP failed" ; $detail ]
    Exit Script [ Text Result: $json ]
End If
Show Custom Dialog [ "Summary" ; JSONFormatElements ( $json ) ]
  1. Run it. Expected JSON, ignoring formatting:
json
{"count":2,"total":37}
  1. Remove a closing bracket from $rows and rerun to exercise the error branch. Restore valid JSON afterward.

Troubleshooting

Use a PHP runtime with the needed extensions enabled. The host converts warnings and notices into exceptions, so an undefined variable may stop a call. A syntax error or missing runtime is available through the captured result and Error. The $rows and $total inside the quoted code are PHP variables; they are not FileMaker variable substitutions.