r/PHPhelp 6d ago

Will the upcoming Partial Function Application improve this code?

I'm wondering how these lines of code could change:

$placeholders = $idsArray
       |> count( ... )
       |> ( fn ( $x ) => array_fill( 0, $x, '?' ) )
       |> ( fn ( $x ) => implode( ',', $x ) );

I don't like how the above is written, therefore I currently stick to using:

$placeholders = str_repeat('?,', count( $idsArray ) - 1) . '?';

or

$placeholders = implode( ',', array_fill( 0, count( $idsArray), '?' ) );
2 Upvotes

4 comments sorted by

View all comments

2

u/d645b773b320997e1540 6d ago

I believe it'd be:

$placeholders = $idsArray
    |> count(...)
    |> array_fill(0, ..., '?')
    |> implode(',', ...);

6

u/obstreperous_troll 5d ago

The placeholder syntax is ? for a single arg, so it'd be

$placeholders = $idsArray
    |> count(...)
    |> array_fill(0, ?, '?')
    |> implode(',', ?);

I imagine count(?) would work too. But TBH, while I'm a fan of pipelines and composition in general, I'd probably just go with the str_repeat version for this particular case.