Help placing {'s

[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]

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

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

Hi Jay,

Thank you for your very detailed reply. I has helped me understand the structure of PHP a bit more.

Thank you for reconstructing my code as well. It makes it a lot easier to understand!

I have replaced the code with yours and I am still receiving an error.

[php]Parse error: syntax error, unexpected $end in /home/signup.php on line 76[/php]

It is pointing to the last line of code for the entire page… Not the last line of PHP. Why is this?

Thanks again

Penny,

Just to make sure the final bracket was placed in the correct place, here is what the code you posted should look like:[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]

I just tested this and it runs fine. If this is what you have and it is giving you the error, then the problem is somewhere else on the page (perhaps a missing bracket from something higher up on the page). If you could post the entire page, I would be happy to look it over for you.

While a missing bracket it the most likely explanation, it is possible that there is something else going on as well.

Thank you!

It is now loading the page with no errors :)!

However, the validation error message is not showing up at all… for any of the fields/errors. I am pretty sure there is nothing wrong with my form as I have used it previously to add data into mysql.

[code]


Create a new account

Username


Password


Re-Type Password


Email


[/code]

I don’t know what could be wrong?

Your form looks OK to me. I used your form and just changed the action to a test page on my server. I then used only the last code that I posted as that page. I gave it a three letter password and it displayed the appropriate error message.

Try creating a test page with only the code from before and change the action on your form to point to this page. See if it works OK like that. If it does, the problem is somewhere else in your yolosignup2.php file.

Something else I noticed when you get it working is that you are not verifying that password and repassword match. I would only add this after the rest is working though.

Let me know if you try the above suggestion and it still doesn’t work.

I did what you said and made new pages for the form and the php code to made sure it worked, which it did. I then took apart the main page piece by piece until it worked.

Thanks for your help guys!

Glad its working for you! Let us know if you run into any other issues or questions.

Best,

jay

Sponsor our Newsletter | Privacy Policy | Terms of Service