wppaste
WordPress

wpdb::get_table_charset( string $table ): string|WP_Error

Since
4.2.0
Source
wp-includes/class-wpdb.php:3276
Retrieves the character set for the given table.

Compatibility

WordPress
since 4.2.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

$tablestring
Table name.

Return value

string|WP_Error
Table character set, WP_Error object if it couldn't be found.

Performance profile

How much work a call to wpdb::get_table_charset() 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
Heavy

Reaches the database via ->get_results().

Scaling
Scales with input

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

Instructions
14–74

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

Plugin surface
1 hook

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

Called by
4

4 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • sqldatabase query->get_results()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 · 6 distinct outcomes

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

WhenInstructionsCalls it makes
always14–19strtolower(), apply_filters()
$charset === null && !isset($tablekey)41strtolower(), apply_filters(), explode(), ->get_results(), __()
$charset === null && !isset($tablekey) && $count !== 153–68strtolower(), apply_filters(), explode(), ->get_results()
$charset === null && !isset($tablekey) && $count === 154–65strtolower(), apply_filters(), explode(), ->get_results(), key()
$charset === null && !isset($tablekey) && !$columns && empty($column)60–61strtolower(), apply_filters(), explode(), ->get_results(), explode(), strtoupper()
$charset === null && !isset($tablekey) && !$columns && !empty($column)73–74strtolower(), apply_filters(), explode(), ->get_results(), explode(), strtolower(), explode(), strtoupper()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev13314–7416
8.513314–7416
8.413314–74163 fewer instructions than PHP 8.3
8.313614–7716
8.213614–7716
8.113614–7716
7.413614–7716

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 wpdb::get_table_charset() runs, in this order:

  1. apply_filters( pre_get_table_charset )filterline 3291 (+15 into the body)

    Filters the table charset value before the DB is checked.

Uses · 4

Used by · 4

Source code

	protected function get_table_charset( $table ) {		$tablekey = strtolower( $table ); 		/**		 * Filters the table charset value before the DB is checked.		 *		 * Returning a non-null value from the filter will effectively short-circuit		 * checking the DB for the charset, returning that value instead.		 *		 * @since 4.2.0		 *		 * @param string|WP_Error|null $charset The character set to use, WP_Error object		 *                                      if it couldn't be found. Default null.		 * @param string               $table   The name of the table being checked.		 */		$charset = apply_filters( 'pre_get_table_charset', null, $table );		if ( null !== $charset ) {			return $charset;		} 		if ( isset( $this->table_charset[ $tablekey ] ) ) {			return $this->table_charset[ $tablekey ];		} 		$charsets = array();		$columns  = array(); 		$table_parts = explode( '.', $table );		$table       = '`' . implode( '`.`', $table_parts ) . '`';		$results     = $this->get_results( "SHOW FULL COLUMNS FROM $table" );		if ( ! $results ) {			return new WP_Error( 'wpdb_get_table_charset_failure', __( 'Could not retrieve table charset.' ) );		} 		foreach ( $results as $column ) {			$columns[ strtolower( $column->Field ) ] = $column;		} 		$this->col_meta[ $tablekey ] = $columns; 		foreach ( $columns as $column ) {			if ( ! empty( $column->Collation ) ) {				list( $charset ) = explode( '_', $column->Collation ); 				$charsets[ strtolower( $charset ) ] = true;			} 			list( $type ) = explode( '(', $column->Type ); 			// A binary/blob means the whole query gets treated like this.			if ( in_array( strtoupper( $type ), array( 'BINARY', 'VARBINARY', 'TINYBLOB', 'MEDIUMBLOB', 'BLOB', 'LONGBLOB' ), true ) ) {				$this->table_charset[ $tablekey ] = 'binary';				return 'binary';			}		} 		// utf8mb3 is an alias for utf8.		if ( isset( $charsets['utf8mb3'] ) ) {			$charsets['utf8'] = true;			unset( $charsets['utf8mb3'] );		} 		// Check if we have more than one charset in play.		$count = count( $charsets );		if ( 1 === $count ) {			$charset = key( $charsets );		} elseif ( 0 === $count ) {			// No charsets, assume this table can store whatever.			$charset = false;		} else {			// More than one charset. Remove latin1 if present and recalculate.			unset( $charsets['latin1'] );			$count = count( $charsets );			if ( 1 === $count ) {				// Only one charset (besides latin1).				$charset = key( $charsets );			} elseif ( 2 === $count && isset( $charsets['utf8'], $charsets['utf8mb4'] ) ) {				// Two charsets, but they're utf8 and utf8mb4, use utf8.				$charset = 'utf8';			} else {

Changelog

Introduced in 4.2.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 7.1.0 tag, from src/wp-includes/class-wpdb.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.