WordPress как на ладони
Недорогой хостинг для сайтов на WordPress: wordpress.jino.ru Получай пассивный доход от сайта с помощью браузерных PUSH уведомлений
функция не описана

WPSEO_Utils::calc() public Yoast 1.5.0

Do simple reliable math calculations without the risk of wrong results.

{} Это метод класса: WPSEO_Utils{}

Хуков нет.

Возвращает

Разное. Calculation Result or false if either or the numbers isn't scalar or an invalid operation was passed.

  • For compare the result will always be an integer.
  • For all other operations, the result will either be an integer (preferred) or a float.

Использование

$result = WPSEO_Utils::calc( $number1, $action, $number2, $round, $decimals, $precision );
$number1(разное) (обязательный)
Scalar (string/int/float/bool).
$action(строка) (обязательный)
Calculation action to execute. Valid input: '+' or 'add' or 'addition', '-' or 'sub' or 'subtract', '*' or 'mul' or 'multiply', '/' or 'div' or 'divide', '%' or 'mod' or 'modulus' '=' or 'comp' or 'compare'.
$number2(разное) (обязательный)
Scalar (string/int/float/bool).
$round(true|false)
Whether or not to round the result. Will be disregarded for a compare operation.
По умолчанию: false
$decimals(число)
Decimals for rounding operation.
$precision(число)
Calculation precision.
По умолчанию: 10

Список изменений

С версии 1.5.0 Введена.
С версии 1.8.0 Moved from stand-alone function to this class.

Код WPSEO_Utils::calc() Yoast 16.2

<?php
public static function calc( $number1, $action, $number2, $round = false, $decimals = 0, $precision = 10 ) {
	static $bc;

	if ( ! is_scalar( $number1 ) || ! is_scalar( $number2 ) ) {
		return false;
	}

	if ( ! isset( $bc ) ) {
		$bc = extension_loaded( 'bcmath' );
	}

	if ( $bc ) {
		$number1 = number_format( $number1, 10, '.', '' );
		$number2 = number_format( $number2, 10, '.', '' );
	}

	$result  = null;
	$compare = false;

	switch ( $action ) {
		case '+':
		case 'add':
		case 'addition':
			$result = ( $bc ) ? bcadd( $number1, $number2, $precision ) /* string */ : ( $number1 + $number2 );
			break;

		case '-':
		case 'sub':
		case 'subtract':
			$result = ( $bc ) ? bcsub( $number1, $number2, $precision ) /* string */ : ( $number1 - $number2 );
			break;

		case '*':
		case 'mul':
		case 'multiply':
			$result = ( $bc ) ? bcmul( $number1, $number2, $precision ) /* string */ : ( $number1 * $number2 );
			break;

		case '/':
		case 'div':
		case 'divide':
			if ( $bc ) {
				$result = bcdiv( $number1, $number2, $precision ); // String, or NULL if right_operand is 0.
			}
			elseif ( $number2 != 0 ) { // phpcs:ignore WordPress.PHP.StrictComparisons -- Purposeful loose comparison.
				$result = ( $number1 / $number2 );
			}

			if ( ! isset( $result ) ) {
				$result = 0;
			}
			break;

		case '%':
		case 'mod':
		case 'modulus':
			if ( $bc ) {
				$result = bcmod( $number1, $number2 ); // String, or NULL if modulus is 0.
			}
			elseif ( $number2 != 0 ) { // phpcs:ignore WordPress.PHP.StrictComparisons -- Purposeful loose comparison.
				$result = ( $number1 % $number2 );
			}

			if ( ! isset( $result ) ) {
				$result = 0;
			}
			break;

		case '=':
		case 'comp':
		case 'compare':
			$compare = true;
			if ( $bc ) {
				$result = bccomp( $number1, $number2, $precision ); // Returns int 0, 1 or -1.
			}
			else {
				// phpcs:ignore WordPress.PHP.StrictComparisons -- Purposeful loose comparison.
				$result = ( $number1 == $number2 ) ? 0 : ( ( $number1 > $number2 ) ? 1 : -1 );
			}
			break;
	}

	if ( isset( $result ) ) {
		if ( $compare === false ) {
			if ( $round === true ) {
				$result = round( floatval( $result ), $decimals );
				if ( $decimals === 0 ) {
					$result = (int) $result;
				}
			}
			else {
				// phpcs:ignore WordPress.PHP.StrictComparisons -- Purposeful loose comparison.
				$result = ( intval( $result ) == $result ) ? intval( $result ) : floatval( $result );
			}
		}

		return $result;
	}

	return false;
}