Jump to content

if else statements


Xtremer360

Recommended Posts

I'm to the point where I want to just have someone scan over this and verify that my code has good syntax and the logic makes sense for each code block. And if not let me know so I can adjust it.

 

<?php

session_start(); // Start a new session

// Include the database page
require ('../inc/dbconfig.php');

if (isset($_POST['submit'])) {

    $errors = "no";
    
    if ((!isset($_POST['username']))||(empty($_POST['username']))||(trim($_POST['username'])=="")||(!isset($_POST['password']))||(empty($_POST['password']))||(trim($_POST['password'])=="")) {
    
        $errors = "yes";
        $message = "The username and password fields were both left blank!";
    
    } else {
    
        if ((!isset($_POST['username']))||(empty($_POST['username']))||(trim($_POST['username'])=="")) {
        
            $errors = "yes";
            $message = "The username was left blank!";
        
        } else {
        
            if ( preg_match("/^[^a-z]{1}|[^a-z0-9]+/i", $_POST['username']) ) {
                
                $errors = "yes";
                $message = "The username contains invalid characters!";
                
            } else {
                
                $username = strtolower($_POST['username']);
                
            }
                
            
        }
        
        if ((!isset($_POST['password']))||(empty($_POST['password']))||(trim($_POST['password'])=="")) {
            
            $errors = "yes";
            $message = "The password was left blank!";
            
        }
    
    }

}

//Output the result
echo $message;

?>

Link to comment
Share on other sites

A few comments, all very minor:

1. For everything that isset() returns true, empty() also returns true. That means you only need to use empty() - using isset() as well is redundant.

2. You don't need a {1} in a regex. It's like saying "I have one single apple": because you said "one" the fact that you have a single apple is a given. In other words, also redundant.

3. PHP has booleans. Use them.

$errors = false;
$errors = true;

4. You've got a lot of parentheses. Not at the "too many" point, but you're getting close.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.