PDO Is this registration secure and well coded?
I don't really like the kind of a review that is popular on this site which only states that your code has some problems but doesn't really offer any solution or improvement. 

Given for some problems listed it's really hard to find a plausible solution, I would prefer an answer where author at least tries to provide a solution, so it will be sort of a sanity check for the problem stated. 

Now to your code. It is pretty secure but not well coded. For me, the main problem here is the code repetition. For some reason each value is accessed and duplicated through many different variables. Why not to keep just one variable initialized at the top? 

Also, empty() is never needed when a variable is deliberately set. You can always use a variable itself for the purpose. 

The PDO code could be also improved, in two ways


Positional placeholders are less verbose and there is no real use fro the named placeholder of only one variable is  going to be used.
Such a manual error handling (i.e. if($stmt = $pdo-&gt;prepare($sql))) is useless and harmful at the same time. An error message like "Oops! Something went wrong." would help noone. Please read my exhaustive explanation on the PHP error reporting for the details.


So here is the 1st tier refactoring, just to make your code tidy and meaningful:

&lt;?php
// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){

    // why not to define a variable already with a value?
    $username = trim($_POST["username"]);
    $email = trim($_POST["email"]);
    $password = trim($_POST["password"]);
    $confirm_password = trim($_POST["confirm_password"]);

    // I would rather make errors an array than separate variables;
    $error = [];

    // Validate username
    if(!$username){
        $error['username'] = "Please enter a username.";
    } else{
        $sql = "SELECT id FROM users WHERE username = ?";
        $stmt = $pdo-&gt;prepare($sql);
        $stmt-&gt;execute([$username]);
        if($stmt-&gt;rowCount()){
            $error['username'] = "This username is already taken.";
        }
    }

    // Validate password
    if(!$password){
        $error['password'] = "Please enter a password.";
    } elseif(strlen($password) &lt; 6) {
        $error['password'] = "Password must have at least 6 characters.";
    }

    // Validate confirm password
    if(!$confirm_password){
        $error['confirm_password'] = "Please enter confirm password.";
    } elseif ($password != $confirm_password){
        $error['confirm_password'] = "Password did not match.";
    }

    // Check input errors before inserting in database
    if(!$error){
        $param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash

        // Prepare an insert statement
        $sql = "INSERT INTO users (username, email,  password) VALUES (:username, :email, :password)";

        $stmt = $pdo-&gt;prepare($sql);
        // Bind variables to the prepared statement as parameters
        $stmt-&gt;bindParam(":username", $username);
        $stmt-&gt;bindParam(":email", $email);
        $stmt-&gt;bindParam(":password", $param_password);

        $stmt-&gt;execute();
        header("location: login");
        die;
    }
}


Just keep in mind that refactoring is an endless process, you can improve any code. but better to do it gradually, in order for you to understand every change as opposed to adding some cargo cult code mindlessly.