wppaste
WordPress

wp_sprintf_l( string $pattern, array $args ): string

Since
2.5.0
Source
wp-includes/formatting.php:5298
Localizes list items before the rest of the content.

Description

The '%l' must be at the first characters can then contain the rest of the content. The list items will have ', ', ', and', and ' and ' added depending on the amount of list items in the $args parameter.

Compatibility

WordPress
since 2.5.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$patternstring
Content containing '%l' at the beginning.
$argsarray
List items to prepend to the content and replace '%l'.

Return value

string
Localized list items and rest of the content.

Performance profile

How much work a call to wp_sprintf_l() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
5–59

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 75.

Plugin surface
1 hook

Third-party callbacks on 'wp_sprintf_l' run inside this call, and their cost is not bounded by anything here.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksapply_filters()called directly

Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 3 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_sprintf_l() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always5–7none
$pattern && !empty($args) && !$args53__(), sprintf(), __(), sprintf(), __(), sprintf(), between(), array_shift()
$pattern && !empty($args) && !$args59__(), sprintf(), __(), sprintf(), __(), sprintf(), between(), array_shift(), array_shift()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev755–595
8.5755–595
8.4755–5956 fewer instructions than PHP 8.3
8.3818–655
8.2818–655
8.1818–655
7.4818–655

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while wp_sprintf_l() runs, in this order:

  1. apply_filters( wp_sprintf_l )filterline 5320 (+22 into the body)

    Filters the translated delimiters used by wp_sprintf_l().

Uses · 3

  • str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
  • apply_filters()Calls the callback functions that have been added to a filter hook.
  • __()Retrieves the translation of $text.

Source code

function wp_sprintf_l( $pattern, $args ) {	// Not a match.	if ( ! str_starts_with( $pattern, '%l' ) ) {		return $pattern;	} 	// Nothing to work with.	if ( empty( $args ) ) {		return '';	} 	/**	 * Filters the translated delimiters used by wp_sprintf_l().	 * Placeholders (%s) are included to assist translators and then	 * removed before the array of strings reaches the filter.	 *	 * Please note: Ampersands and entities should be avoided here.	 *	 * @since 2.5.0	 *	 * @param array $delimiters An array of translated delimiters.	 */	$l = apply_filters(		'wp_sprintf_l',		array(			/* translators: Used to join items in a list with more than 2 items. */			'between'          => sprintf( __( '%1$s, %2$s' ), '', '' ),			/* translators: Used to join last two items in a list with more than 2 times. */			'between_last_two' => sprintf( __( '%1$s, and %2$s' ), '', '' ),			/* translators: Used to join items in a list with only 2 items. */			'between_only_two' => sprintf( __( '%1$s and %2$s' ), '', '' ),		)	); 	$args   = (array) $args;	$result = array_shift( $args );	if ( count( $args ) === 1 ) {		$result .= $l['between_only_two'] . array_shift( $args );	} 	// Loop when more than two args.	$i = count( $args );	while ( $i ) {		$arg = array_shift( $args );		--$i;		if ( 0 === $i ) {			$result .= $l['between_last_two'] . $arg;		} else {			$result .= $l['between'] . $arg;		}	} 	return $result . substr( $pattern, 2 );}

Changelog

Introduced in 2.5.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/formatting.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.