Oihana PHP

compile.php

Table of Contents

Functions

compile()  : string
Compiles a string, object, boolean, or an array of expressions into a single string.

Functions

compile()

Compiles a string, object, boolean, or an array of expressions into a single string.

compile(string|Stringable|array<string|int, mixed>|null $expressions[, string $separator = ' ' ][, callable|null $callback = null ]) : string

Behavior:

  • If the input is an array, the values are cleaned with clean() (removing empty or null entries), optionally transformed with a callback, then recursively compiled and concatenated using the specified separator.
  • If the input is a string, it is returned as is.
  • If the input is an object implementing Stringable, it is cast to string.
  • If the input is any other object, it is converted to JSON using json_encode.
  • Booleans are converted to 'true' or 'false'.
  • Null or unsupported types are converted to an empty string.
Parameters
$expressions : string|Stringable|array<string|int, mixed>|null

The expression(s) to compile.

$separator : string = ' '

The separator used when joining array values (default is a single space).

$callback : callable|null = null

An optional callback applied to each array element before compiling. Signature: function(mixed $item): mixed.

Tags
author

Marc Alcaraz

since
1.0.0
example
use function oihana\core\expressions\compile;

echo compile( [ 'foo' , '' , 'bar' ] )        . PHP_EOL; // 'foo bar'
echo compile( [ 'a'   , null , 'c' ] , ', ' ) . PHP_EOL; // 'a, c'
echo compile( 'baz' )                         . PHP_EOL; // 'baz'
echo compile( null )                          . PHP_EOL; // ''
echo compile([1, 2, 3], '-', fn($v) => $v*2)  . PHP_EOL; // '2-4-6'
Return values
string

The compiled string expression.


        
On this page

Search results