Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I was trying to find a quick and easy method to check if the first letter in a string is a number. A lot of the functions and methods I've seen on S.O seem over complicated. I'm wondering, would something like this work:

is_numeric($string[0]);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
251 views
Welcome To Ask or Share your Answers For Others

1 Answer

An easier way might be:

is_numeric(substr($string, 0, 1))

It tackles the problem of a possible empty string (that has no first character) by using substr(). substr() returns false in the case of an empty string, and false is not recognized as a number by is_numeric().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...