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
https://forums.phpfreaks.com/topic/239009-if-else-statements/
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
https://forums.phpfreaks.com/topic/239009-if-else-statements/#findComment-1228128
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.