PHP: convert string to float (only if string represents a float)

前端 未结 5 902
隐瞒了意图╮
隐瞒了意图╮ 2021-01-16 08:59

I have an unknwon string that could resemble a float. In that case I want to convert it to float (for calculations), otherwise leave it as a string.

5条回答
  •  囚心锁ツ
    2021-01-16 09:29

    You can use the following to check if a string is a float:

    $a = "1.23";
    $isFloat = ($a == (string)(float)$a);
    

提交回复
热议问题