Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Help placing {'s

    PHP Code:
    <?php
    if (isset($_POST['username'], $_POST['password'], $_POST['repassword'], $_POST['email'])) {
    $errors = array();

    $username $_POST['username'];
    $password $_POST['password'];
    $repassword $_POST['password'];
    $email $_POST['email'];

    if (empty(
    $username) || empty($password) || empty($repassword)|| empty($email)) {
    $errors[] = 'All fields are required';
    }
    else {

    if (
    strlen($username) > 25 )
    $errors[] = 'Username can not be longer than 12 characters';



    if (
    strlen($username) < )
    $errors[] = 'Username must be 3 characters or more';



    if (
    strlen($password) < )
    $errors[] = 'Password must be 5 characters or more';


    if (
    filter_var($emailFILTER_VALIDATE_EMAIL) === FALSE)
    $errors[] = 'Please enter a valid email address';
    }

    if (!empty(
    $errors)) {
    foreach (
    $errors as $error) {

    echo 
    $error;

    }}

    ?>
    I am not sure if I am placing the {'s correctly.. Please can someone check if it is correct and explain when and where I need to use them?

    Thanks
    Last edited by penny94; 10-01-2012 at 06:23 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Looks good to me. Indenting will go leaps and bounds on readability:
    PHP Code:
     <?php
    if (isset($_POST['username'], $_POST['password'], $_POST['repassword'], $_POST['email']))
    {
        
    $errors = array();

        
    $username $_POST['username'];
        
    $password $_POST['password'];
        
    $repassword $_POST['password'];
        
    $email $_POST['email'];

        if (empty(
    $username) || empty($password) || empty($repassword)|| empty($email))
        {
            
    $errors[] = 'All fields are required';
        }
        else
        {
            if (
    strlen($username) > 25 )
                
    $errors[] = 'Username can not be longer than 12 characters';

            if (
    strlen($username) < )
                
    $errors[] = 'Username must be 3 characters or more';

            if (
    strlen($password) < )
                
    $errors[] = 'Password must be 5 characters or more';

            if (
    filter_var($emailFILTER_VALIDATE_EMAIL) === FALSE)
                
    $errors[] = 'Please enter a valid email address';
        }

        if (!empty(
    $errors))
        {
            foreach (
    $errors as $error)
            {
                echo 
    $error;
            }
        }
        else
        {
            echo 
    'woop';
        }
    }
    ?>
    I'd also suggest to always use blocks for the conditional checks instead of next instruction implicit check. Its easy to make an error by adding an instruction and then not knowing why its always set. It is up to you on that one though.

    The foreach can also be skipped and you can use an implode on the $errors instead to give it a separator.

    Edit:
    Wait, you may have one off here. The else for the 'woop' may or may not be any good, but there's no clear indication what that should be. That else block would indicate that everything is fine.
    You may want to add another else to indicate that it didn't receive all the fields required.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •