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']).')';
}
4
  • 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. Commented Sep 1, 2018 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. Commented Sep 1, 2018 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. Commented Sep 1, 2018 at 12:40
  • 1
    Possible duplicate of Warning: A non-numeric value encountered Commented Sep 1, 2018 at 12:48

3 Answers 3

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 :) ).

Sign up to request clarification or add additional context in comments.

Comments

1

Variables aren't numeric. Use:

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

invtal(); PHP Documentation

Comments

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'])).'%'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.