在一个用divi制作的wordpress网站中,在divi主题包含生成器文件夹的functions.php
文件中,我得到了一个间歇性的错误:
$value = substr( $value, 1, $value_length ) . trim();
错误提示Warning: trim() expects at least 1 parameter, 0 given
这是因为里面没有装饰吗?但是像这样把它链在末端它应该指的是它被链在上面的东西,对吗?所以是$value
的substr
。这是否意味着$value
只是没有给出?或者当你得到substr
时,那里没有别的东西了?
希望使用javascript的console.log来看看现在到底有什么。是否有一个php等效的?还是开发工具的php插件让我随时都能看到变量?
有人知道这里的问题是什么吗?我甚至不确定这段代码在此刻做什么,我猜是在一些文本上设置css px值……,并删除!important
属性?
if ( ! function_exists( 'et_pb_get_value_unit' ) ) :
/**
* Get unit of given value
*
* @param string $value string with unit.
* @param string $default_unit default unit.
*
* @return string unit name
*/
function et_pb_get_value_unit( $value, $default_unit = 'px' ) {
$value = isset( $value ) ? $value : '';
$valid_one_char_units = array( '%', 'x' );
$valid_two_chars_units = array( 'em', 'px', 'cm', 'mm', 'in', 'pt', 'pc', 'ex', 'vh', 'vw', 'ms' );
$valid_three_chars_units = array( 'deg', 'rem' );
$important = '!important';
$important_length = strlen( $important );
$value_length = strlen( $value );
if ( '' === $value || is_numeric( $value ) ) {
return $default_unit;
}
if ( substr( $value, ( 0 - $important_length ), $important_length ) === $important ) {
$value_length = $value_length - $important_length;
$value = substr( $value, 1, $value_length ) . trim();
}
if ( in_array( substr( $value, -3, 3 ), $valid_three_chars_units, true ) ) {
return substr( $value, -3, 3 );
}
if ( in_array( substr( $value, -2, 2 ), $valid_two_chars_units, true ) ) {
return substr( $value, -2, 2 );
}
if ( in_array( substr( $value, -1, 1 ), $valid_one_char_units, true ) ) {
return substr( $value, -1, 1 );
}
return $default_unit;
}
endif;
这样的链接是其他语言(如JavaScript)的特性,但它是而不是PHP的一个特性。正如它所写的,你正在连接一个'修剪的null
'"放到值的末尾,这是没有意义的。
查看trim()
函数文档-string
参数是必需的,因此它知道要修剪什么。
如果你想"chain"像这样,你实际上需要包含trim
的值,像这样:
$value = trim( substr( $value, 1, $value_length ) );
你可能会看到"chained"像这样的函数被称为"嵌套的"。
就代码所做的而言,这是一种相对复杂的从值返回单位类型的方式-可能是为了查看它是否为rem
,px
,%
-等等,以便在其他地方使用。它可以简化很多(也许是用regex简化一行),但这超出了这个问题的范围