I am trying to test a variable if the value is alpha numeric or not.. but I don't know how.. For example if the value of a variable is abc123 the result is true but if the value of the variable is abc@8%_ then I will get a false result.. e.g. $a = "abc123"; if($a==alphanumeric) then true $a = "Ab$(" if($a==alphanumeric) then false I hope my question not very hard to understand
dude php have a native function for that ctype_alnum -- Check for alphanumeric character(s) so pwede mo gawin ito... (you can try this) if (ctype_alnum($value) ) { true statement here } else { false statement here }
you can also use regular expression for these kind of things fot this specil request reg exp would be preg_match("/^([A-Za-z0-9]+)$/", $value)
What yleiko said, though it's best/faster to just stick to native functions when you can. I used to use a regex expression to make sure something was all numbers and then I realized I should just be using ctype_digit().
Sure, there is no need to use reg exp when there is a native function, but native functions are not always enough for your needs. Say you need to check whether it is alphanumeric and plus some special characters (language specific characters or some other characters). You need to reg exp. My suggestion is for this case, use ctype_alnum function, but if you will continue coding on php (or any other language), learn reg exp. Most of the time, it saves lots of time and hundreds of coding.