Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if variable is Alphabetic or Numeric in PHP?

Tags:

php

How to check if variable is Alphabetic or Numeric in PHP?

like image 442
Steven Hammons Avatar asked Mar 15 '11 05:03

Steven Hammons


1 Answers

Depending on how you define numeric, you'll use one of the following functions :

  • is_numeric
  • ctype_digit

With the first one, numeric is defined as (quoting) :

Numeric strings consist of optional sign, any number of digits, optional decimal part and optional exponential part.

While, with the second one, you will (quoting) :

Checks if all of the characters in the provided string, text, are numerical


And, for alphabetic, you'll be interested by :

  • ctype_alpha

Quoting :

Checks if all of the characters in the provided string, text, are alphabetic. In the standard C locale letters are just [A-Za-z]


And, as pointed out by @Long Ears in his comment, if you want to check both in a single shot, you'll find the ctype_alnum() function (quoting) :

Checks if all of the characters in the provided string, text, are alphanumeric.


In any case, you might want to take a look at the full list of Ctype functions.

like image 199
Pascal MARTIN Avatar answered Oct 06 '22 01:10

Pascal MARTIN