-2

i do have really similar issues as in this topic: Proper way of converting string to long int in PHP

Unfortunately solution there is not working for me.

My situation is following:

I do have Wordpress database with meta_key which value is 700 (in LONGTEXT). I would like to get this number as Integer.

But when I use intval($meta_value) or even (float) $meta_value it adds extra zero to at the end and returns 7000 :(

Could someone please help?

EDIT: Screenshots added

here is picture of data in database here is picture of data in database

here is code that loads this value and converts it here is code that loads this value and converts it

But the site is still returning 7000

Here is original code:

$meta_value = the_author_meta('pocet_bodu', $user_id);
                                    echo $meta_value;

This returns okay 700, but it is not number and it cannot be used for mathematical operations. Once I try to convert it to integer or float, number is 7000.

Community
  • 1
  • 1
snorbik
  • 107
  • 1
  • 9
  • 2
    I have never heard of casting a long to an int resulting in multiplication by 10. Can you show us some actual data here? – Tim Biegeleisen Jul 24 '16 at 13:58
  • What if you cast it like this: `(int) $meta_value` – steven Jul 24 '16 at 14:00
  • imo, I would: debugging: `var_dump($meta_value, bin2hex($meta_value), strlen($meta_value), intval($meta_value), (float) $meta_value, __FILE__.__LINE__));` Why? See what the actual data really is, not just the printable characters. Also, it is quick to do and easy to see what is happening. – Ryan Vincent Jul 24 '16 at 14:08
  • Neither `(int) $meta_value` is not working for me. Screenshots of code added – snorbik Jul 24 '16 at 14:09
  • Take a look at the value before casting! Do a var_dump! – steven Jul 24 '16 at 14:13
  • 1
    Ok, where does the 7000 come from? Not from $meta_value obviously? – Ryan Vincent Jul 24 '16 at 14:15
  • This is what `var_dump` returns `NULL string(0) "" int(0) int(0) float(0) string(90)` `$meta_value` in my code is `$bodyUzivatele` – snorbik Jul 24 '16 at 14:16
  • 3
    Post your code as plain text, not images. – Barmar Jul 24 '16 at 14:35

1 Answers1

1

This was a fun problem. Kindly change the_author_meta() to get_the_author_meta().

The former function will echo "700" (with no return value) and then you additionally echo (int) on that non-return value (effectively (int)false, if I'm not mistaken, which is 0). There is your 7000.

Drakes
  • 23,254
  • 3
  • 51
  • 94