How to check if a variable is Alphabetical or numeric in PHP?

How to check if a variable is Alphabetical or numeric in PHP?

+4
source share
4 answers

Depending on how you define the numerical value, you will use one of the following functions:

With the first, numerical is defined as (quoting):

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

So far, with the second, you will be (citing):

Checks if all characters in if string, text, numeric


And, for the alphabet, you will be interested in:

Quote:

Checks if all characters in text, text, are alphabets. In standard C characters, just [A-Za-z]


And, as @Long Ears noted in his comment, if you want to check both in the same frame, you will find ctype_alnum() (quoting):

Checks if all characters in a string, text, alphanumeric.


In any case, you can take a look at the full list of Ctype features .

+9
source

Use the is_numeric function:

 is_numeric("42"); // true is_numeric(1337); // true is_numeric("1e4"); // true is_numeric("not numeric"); // false is_numeric(Array()); // false is_numeric(9.1); // true 
+4
source

You can use is_numeric () or ctype_alpha () functions

+1
source
 you quick test the input val by if numeric or alpha`enter code here` using this php function if(ctype_alnum($eafrik)){ echo "is alphatnumeric"; }else{ echo "is not alphanumeric" } 
-2
source

Source: https://habr.com/ru/post/1343720/


All Articles