Jump to content

When to use { }


baser-b

Recommended Posts

I am working with some code, and am confused as why it doesn't show an error

    {    
       $lastname = sanitizeString($_POST['lastname']);
    
       if ($result->num_rows)         
            queryMysql("UPDATE accounts SET lastname='$lastname' WHERE user='$user'");
          else queryMysql("INSERT INTO accounts (user, lastname) VALUES('$user', '$lastname')");  
    }  
       else  
       {    
          if ($result->num_rows)    
          {          
              $lastname = stripslashes($row['lastname']);    
          }    
             else $lastname = "";  
        }

Why does it have else and if with brackets in some of the code but not other parts? How do I know when to use them?

Link to comment
Share on other sites

{}s are optional if you only have one statement. That one statement can be another complex structure, which means this is valid:

if ($condition)
	if ($other_condition) {
		function_1();
		function_2();
	}

If you want advice about when to use them, always use them. They help with readability and reduce the odds of making bugs because of confusing indentation. They're also virtually required if you write something like

if ($condition_1) if ($condition_2) echo "conditions 1 and 2 are true"; else echo "condition 1 is false... or is it condition 2 is false?";

 

Link to comment
Share on other sites

Think of the braces as "grouping operators".  When you need to collect a few lines (or more) as one group, then wrap them in braces.  This is most often used as part of an if/else construct where you need to be sure that the lines you want executed for the if condition of for the else condition must all happen.  That is why it is a good practice to always use them in an if/else statement.  Sooner or later you will add one more line to the if or to the else and forget to add the braces and then wonder why your newly-fixed code doesn't do what you think it should do.

Notice how braces are used when writing a function.  After the function header line that identifies the function there is always a set of braces to 'group' the following lines  together as the block of code used in that function.   Does that help?

Link to comment
Share on other sites

Well I keep getting an error with this code, saying the 'else' is wrong.

   function showCollaborators($user)
   {
      $entry = queryMysql("SELECT collaborator FROM users WHERE user='$user'");
      
      echo "Collaborator(s): ";
         
         if ($entry->num_rows > 0)
         {
            while ($row = $entry->fetch_assoc() && $entry->num_rows > 1)
            {
               $row['name'] = $view;
               echo "<a href='users.php?view=$view'>" . $view . "</a>, ";
            }
               else 
               {
                  echo "<a href='users.php?view=$view'>" . $view . "</a>.";
               }
         }
   }

 

Link to comment
Share on other sites

Look at it without the while:

   function showCollaborators($user)
   {
      $entry = queryMysql("SELECT collaborator FROM users WHERE user='$user'");
      
      echo "Collaborator(s): ";
         
         if ($entry->num_rows > 0)
         {
               else 
               {
                  echo "<a href='users.php?view=$view'>" . $view . "</a>.";
               }
         }
   }

 

Link to comment
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.