Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if first character of a string is letter or number in PHP? [duplicate]

Tags:

php

Is there a way to check if the first character of a string is a letter or a number? I'm not really sure what function to use. Is there a way to check not using regex, as we have not learned that yet in my class.

like image 652
Gurpal Rattu Avatar asked Jan 09 '23 17:01

Gurpal Rattu


1 Answers

I'd encourage you to read more about strings in PHP. For example, you can dereference them like arrays to get individual characters.

$letters = 'abcd';
echo $letters[0];

There are also a handful of ctype functions. Check out, ctype_digit() and [ctype_alpha()}(http://php.net/manual/en/function.ctype-alpha.php).

ctype_digit($letters[0]); // false
ctype_alpha($letters[0]); // true

Putting these together, you should be able to do what you want.

like image 94
Jason McCreary Avatar answered Jan 22 '23 20:01

Jason McCreary