Validating username fields and showing CAPTCHA I'm new to coding, and especially to OOP PHP. I'll highly appreciate if you expert programmers could review following code and give me your opinion to make it better. Also, how you see this code, is it too horrible and ugly?

&lt;?php
include_once("config.inc.php");

class user{

    var $mysqli;
    var $username;
    var $userid;

     function __construct($mysqli) {
         session_start(); 
         $this-&gt;mysqli = $mysqli;
    }

    function valid_length($input){
        if ((strlen($input) &gt; 4 ) AND (strlen ($input) &lt; 16) ){
            return true;    
        } else {
            return false;
        }   
    }

    function valid_email($email){
        if (preg_match("/^[0-9a-z]+(([\.\-_])[0-9a-z]+)*@[0-9a-z]+(([\.\-])[0-9a-z-]+)*\.[a-z]{2,4}$/i" , $email)) {
            return true;
        } else {
            return false;
        }
    }

    function alpha_numeric($input){
        if (eregi("^([0-9a-z])+$", $input)){
            return true;        
        }else{
            return false;       
        }   
    }

    function username_exists($username){

        if ($stmt = $this-&gt;mysqli-&gt;prepare("SELECT username FROM users WHERE username=?")) {
            $stmt-&gt;bind_param("s", $username);
            $stmt-&gt;execute();
            $stmt-&gt;store_result();
            $count=$stmt-&gt;num_rows;
            $stmt-&gt;close();
        }
        return ($count &gt; 0 ? true : false);
    }

    function signup($username, $password, $email, $capt_error){

    //validate username.
    if(!$this-&gt;valid_length($username)){
            $this-&gt;signup_error = "Username should be 5 - 15 characters. &lt;br /&gt;";   
    }else if(!$this-&gt;alpha_numeric($username)){ 
            $this -&gt; signup_error ="Only letters, numbers";     
    } else if($this-&gt;username_unallowed($username)){ 
            $this -&gt; signup_error ="Not allowed.";
    } else if ($this-&gt;username_exists($username)){
            $this -&gt; signup_error ="Username already exists. &lt;br /&gt;";
    }

    //validate password.
   if (!$this-&gt;valid_length($password) ){   
            $this -&gt; signup_error .="Password should be 5 - 15 characters. &lt;br /&gt;";
        }

    //validate email.
    if (!$this-&gt;valid_email($email) ){  
            $this -&gt; signup_error .="Invalid email.";
    }else if($this-&gt;email_exists($email)) {
            $this -&gt; signup_error .="Email exists.&lt;br /&gt;";  
        }

    //captcha error
    if($capt_error == 1){
    $this -&gt; signup_error .="Invalid captcha. &lt;br /&gt;";  
    }

    //no any error, proceed.
    if ($this-&gt;signup_error == ""){
        $hashed_password = $this-&gt;hash_password($password);
        if ($this-&gt;insert_new_user($username, $enc_password, $email)){
        $_SESSION['username']=$username;    
        header("Location: welcome.php");
        }else {
        $this-&gt;signup_error = "Unknown error. &lt;br /&gt;";
        }       
    }

  }


}
?&gt;
Why not use the built-in PHP e-mail validation?

filter_var($email, FILTER_VALIDATE_EMAIL)


Why do

if (lol())
  return true;
else
  return false;


when you can do

return lol();


Same goes for lol() ? true : false which you do too.

Furthermore the signup_error stuff is ugly. I'd suggest exceptions. At least make signup_error into an array instead, and just add new elements when you get an error. You can join() the array when outputting.

Also, you risk $count not being defined if $stmt fails.