回显一个intval创建了空白的负载

I have a variable that is a long decimal (2.34353) and I want to round it to 2. So I am using intval on the variable. I then echo the intval and the value is correct, except that it spits out a bunch of whitespace before and after the value. In a var_dump it only shows the number, therefore (unless var_dump never shows whitespace) I am assuming echo'ing it is adding whitespace for some reason.

I tried a str_replace and preg_replace on the variable before the echo but the problem remains. I am developing on Wordpress, so I have a feeling it might be the culprit.

Any ideas on what to try next? Thanks

Try using the round() function

echo trim(intval($myvar)); // should clear out any wrapping whitespace
  1. Intval is not a rounding function. It just drops the fractional part of a number. Use round for rounding.

  2. There is no problem with echo or with intval. Probletm is out there. Try to echo empty string echo '';, and to look what you get.