简体   繁体   中英

PHP converting String to Integer and knowing when value is not Integer in String format

In Java you can try to convert any String value to Integer, but when value is something else, it will throw NumberFormatException . Is there similar functionality in PHP? I checked intval , but it returns values when String value obviously is not convert-able value.

A language throwing an error is not really a useful feature, it's not what you're actually trying to do.
I assume that you simply want to figure out if your string is numeric . For that there's is_numeric or ctype_digit .

您可以使用is_numeric()检查它,然后抛出一个exeption

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM