Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-01-2012, 06:08 PM   PM User | #1
penny94
New Coder

 
Join Date: Sep 2012
Posts: 21
Thanks: 3
Thanked 0 Times in 0 Posts
penny94 is an unknown quantity at this point
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..
penny94 is offline   Reply With Quote
Old 10-01-2012, 06:24 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:05 AM.


Advertisement
Log in to turn off these ads.