If you want to validate that the entire string only consists of 1 or more alpha-numeric characters, then you need to "anchor" the ends of the pattern to the beginning ("") and end ("$") of the string. Otherwise you are just validating that the pattern matches any part of the string being tested.
if(!ereg('^[a-zA-Z0-9]+$', $string)) {
//
}
The reason error 3 printed is that you had "$b" as the string, but presumably the variable $b was undefined, so it was an empty string, and thus no part of that (empty) string matched the pattern.
Note that the preg() family of functions are generally preferred over the slower and less powerful ereg() family.:
if(!preg_match('/^[a-z0-9]$/i', $string)) {
However, in this case you can use the even simpler and faster ctype*() family of functions:
if(!ctype_alnum($string)) {