0

I have a problem in my php code. does anyone know how to fix this? Help me please!

Warning: A non-numeric value encountered in C:\xampp\htdocs\reza\wp-content\plugins\trx_addons\shortcodes\promo\tpl.default.php on line 24

This is line 24:

? (100 - $args['gap'] - (int) str_replace('%', '', $args['image_width'])).'%'

Whole code:

$args = get_query_var('trx_addons_args_sc_promo');

$args['image'] = trx_addons_get_attachment_url($args['image'], 'full');
if (empty($args['image'])) {
    $args['image_width'] = '0%';
    $text_width = "100%";
} else if (empty($args['title']) && empty($args['subtitle']) && empty($args['description']) && empty($args['content']) && (empty($args['link']) || empty($args['link_text']))) {
    $args['image_width'] = '100%';
    $text_width = 0;
} else {
    $args['gap'] = trim(str_replace('%', '', $args['gap']));
    if (!empty($args['gap']) && strpos($args['image_width'], '%')!==false)
        $args['image_width'] = ((int) str_replace('%', '', $args['image_width']) - $args['gap']/2) . '%';
    $text_width = strpos($args['image_width'], '%')!==false
            ? (100 - $args['gap'] - (int) str_replace('%', '', $args['image_width'])).'%'
            : 'calc(100%-'.($args['gap'] ? $args['gap'].'%' : '').trim($args['image_width']).')';
}
Andreas
  • 23,610
  • 6
  • 30
  • 62
Reza Gm
  • 1
  • 1
  • 1
  • You are trying to run math on strings. `var_dump(args['gap'])` gives what? I'd guess data type is string. Cast it if it should always be an integer. – user3783243 Sep 01 '18 at 12:31
  • A good thing to remember is to not only give the code that creates the error, but also the variables that create the error. Do a var_dump of each of the variables and post them in your question. – Andreas Sep 01 '18 at 12:38
  • There is lots of str_replace of "%" to "". Do them once and save the value. You seem to do them everywhere in your code. – Andreas Sep 01 '18 at 12:40
  • 1
    Possible duplicate of [Warning: A non-numeric value encountered](https://stackoverflow.com/questions/42044127/warning-a-non-numeric-value-encountered) – coderroggie Sep 01 '18 at 12:48

3 Answers3

1

It is new type of warning in PHP 7.1 (http://php.net/manual/en/migration71.other-changes.php)

Either $args['gap'] or $args['image_width'] is not numeric or is not initialized (which is not numeric too :) ).

Tunker
  • 117
  • 9
1

Variables aren't numeric. Use:

? (100 - intval($args['gap']) - intval(str_replace('%', '', $args['image_width']))).'%' 

invtal(); PHP Documentation

Jack Glow
  • 51
  • 1
  • 13
1

I had this same issue just now with the exact same plugin.

? (100 - $args['gap'] - (int) str_replace('%', '', $args['image_width'])).'%'

should be

? (100 - (int) $args['gap'] - (int) str_replace('%', '', $args['image_width'])).'%'

Rob Conley
  • 11
  • 1