0

Warning: A non-numeric value encountered in line 107.

<div class="like">
    <?php 
        $nonce = wp_create_nonce("my_user_vote_nonce");
        $likeLink = admin_url('admin-ajax.php?action=my_user_vote&custom=like&post_id='.$post->ID.'&nonce='.$nonce);            
    ?>
    <a class="like" data-image="<?php bloginfo('template_url');?>/images/loading.gif" data-field="like" data-nonce="<?php echo $nonce ?>" data-post_id="<?php echo $post->ID ?>" href="<?php echo $link ?>" title="Like"><span><?php echo number_format(get_post_meta($post->ID, "like", true) * 1, 0, ',', '.'); ?></span></a>
</div>

line 107

<a class="like" data-image="<?php bloginfo('template_url');?>/images/loading.gif" data-field="like" data-nonce="<?php echo $nonce ?>" data-post_id="<?php echo $post->ID ?>" href="<?php echo $link ?>" title="Like"><span><?php echo number_format(get_post_meta($post->ID, "like", true) * 1, 0, ',', '.'); ?></span></a>
  • You have a lot of stuff on that line, can you split it into multiple lines so that we can figure out which part is the problem? I have a suspicion where it is. Otherwise, are you asking where the warning is, what the warning means, or are you asking how to fix it? – Tom J Nowell Mar 13 at 12:06
  • Thank you Tom for your help, i want to fix it. Why it is happening with PHP 7.1. – Ersin Mar 13 at 12:13
1

I suspect the problem is this:

echo number_format(get_post_meta($post->ID, "like", true) * 1, 0, ',', '.')

Here, we take the post meta named like and multiply it by 1, but what if that post meta doesn't contain a number? What if it contains something else?

For example, if we did 2 x 4, we would expect PHP to give us 8, which is correct, but what if we tell PHP to multiple 2 x "hello"? or 🤔 - 📝? When that happens PHP will throw a warning, the same one that you are encountering.

So first, don't just smush everything on to one line, use a variable to store the post meta output:

$like = get_post_meta( $post->ID, "like", true );

Then use that variable instead:

echo number_format($likes, 0, ',', '.')

Now that we've done that, it's possible to check and format the variable as a number:

if ( !is_numeric( $likes ) ) {
    $likes = 0;
}

So if $likes is a false or an error value, we set it to 0 rather than just blindly using it and hoping for the best.

And finally, that line has a lot of things going on, so this might not be the issue causing the warning at all, it's not possible to tell. In future instead of:

    <a class="like" data-image="<?php bloginfo('template_url');?>/images/loading.gif" data-field="like" data-nonce="<?php echo $nonce ?>" data-post_id="<?php echo $post->ID ?>" href="<?php echo $link ?>" title="Like"><span><?php echo number_format(get_post_meta($post->ID, "like", true) * 1, 0, ',', '.'); ?></span></a>

Do:

    <a
        class="like"
        data-image="<?php bloginfo('template_url');?>/images/loading.gif"
        data-field="like"
        data-nonce="<?php echo $nonce ?>"
        data-post_id="<?php echo $post->ID ?>"
        href="<?php echo $link ?>"
        title="Like"
    >
        <span>
            <?php echo number_format(get_post_meta($post->ID, "like", true) * 1, 0, ',', '.'); ?>
        </span>
    </a>

The above snippet does not contain any fixes, but, it's easier to read, and easier to debug, taking all the guesswork out of which part of the line has the problem.

| improve this answer | |
  • 1
    It worked flawlessly ! Thank you. – Ersin Mar 13 at 12:50
  • Glad you got it working :) If this answers your question can you mark it as the correct answer? It's the big tick just under the voting arrows to the left of the answer – Tom J Nowell Mar 13 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.