crayon-syntax-highlighter代码高亮插件Notice问题解决
在调试redis的时候,打开了wordpress的debug,看到了crayon-syntax-highlighter代码高亮插件出现了以上这个警告提示,其实也不影响功能的使用,就是看着不爽(^_^我有强迫症)。索性,花了点时间解决一下这个告警提示。

PHP7.1官方文档解释

New E_WARNING and E_NOTICE errors have been introduced when invalid strings are coerced using operators expecting numbers (+ - * / ** % << >> | & ^) or their assignment equivalents. An E_NOTICE is emitted when the string begins with a numeric value but contains trailing non-numeric characters, and an E_WARNING is emitted when the string does not contain a numeric value.

在使用(+ - * / ** % << >> | & ^) 运算时,例如a+b,如果a是开始一个数字值,但包含非数字字符(123a),b不是数字值开始时(b456),就会有A non-numeric value encountered警告。

解决过程

打开报错文件crayon_formatter.class.php,打印一下变量的数值,看是什么东西。

118             $toolbar_height = $font_size * 1.5 . 'px !important;';
119             var_dump($font_size);
120             echo '
'; 121 var_dump($toolbar_height); 122 exit();

打印出来的结果看到$font_size的值是string类型,字符串和数值进行乘法运算,肯定是要报Notice的。

Notice: A non well formed numeric value encountered in /data/wwwroot/www.lianst.com/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118
string(16) "12px !important;" 
string(16) "18px !important;"

我们来使用intval方法把$font_size强制转换成数值之后再做乘法运算,应该就木有问题可。

118             $toolbar_height = $font_size * 1.5 . 'px !important;';
119             var_dump(intval($font_size));

使用intval方法把$font_size强制转换成数值之后可以看到$font_size的值已经转换成int类型了。

Notice: A non well formed numeric value encountered in /data/wwwroot/www.lianst.com/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118
int(12) 

解决方法

进入到crayon-syntax-highlighter插件的目录下面,找到crayon_formatter.class.php文件,将其报错行(118、119)处修改为如下代码即可。

118             $toolbar_height = intval($font_size) * 1.5 . 'px !important;';
119             $info_height = intval($font_size) * 1.4 . 'px !important;';

注:
1.参考资料:傲雪星枫CSDN

文章目录