Hi Penny94,
You need one more bracket at the end of your code (to balance with the second line: if (isset…)
There are some places where you don’t need to use the brackets (although they aren’t hurting anything. There are also places you could put brackets, but you don’t need to.
In the code you provided, the purpose for brackets is to group together all of the items that depend on one of the conditions. For instance, you have a left bracket at the end of your initial if statement on line 2. This means that everything between this bracket and the corresponding right bracket (which is missing in your code) should only be executed when the if condition(s) are true. For example, if any of the listed $_POST variables is not set, nothing in between the matching brackets will be executed.
I have restructured your code so that you can visually see the matching brackets:[php]<?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) < 3 ) $errors[] = 'Username must be 3 characters or more';
if(strlen($password) < 5 ) $errors[] = 'Password must be 5 characters or more';
if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE) $errors[] = 'Please enter a valid email address';
}
if(!empty($errors))
{
foreach($errors as $error)
{
echo $error;
}
}
?>[/php]
This makes it fairly easy to see that the first bracket doesn’t have a matching right bracket (it should be lined up in the same column). This bracket should be added at the bottom of this code. To keep the visual balancing, you would insert it just above the php end tag (?>) and put two spaced before it.
An example of an unneeded bracket is here:[php] if (empty($username) || empty($password) || empty($repassword)|| empty($email))
{
$errors[] = ‘All fields are required’;
}[/php]
While it is perfectly fine to use the brackets here, you could also write this as:[php]if (empty($username) || empty($password) || empty($repassword)|| empty($email))
$errors[] = ‘All fields are required’;[/php]
I personally like it better the way you have it, but to each their own. The brackets would only be needed here if you had multiple things that should only happen if the if condition(s) were true.
By the same token, you could use brackets after each of your if statements that appear in the else clause.
One final note: although you only asked about the brackets, an easier way to display the $errors array would be to replace this:[php]if(!empty($errors))
{
foreach($errors as $error)
{
echo $error;
}
}[/php]
With this:[php]if(!empty($errors)) echo implode(’
’,$errors);[/php]
Implode makes a flat string out of all of the elements of an array. It inserts the first argument between each instance. In this case it will output each array element and insert ‘
’ (the html line break) between each one.
Hope this helps. Let me know if it doesn’t make sense, or if it doesn’t work for you with the final bracket added.
jay