baser-b Posted October 7, 2018 Share Posted October 7, 2018 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? Quote Link to comment Share on other sites More sharing options...
requinix Posted October 7, 2018 Share Posted October 7, 2018 {}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?"; Quote Link to comment Share on other sites More sharing options...
ginerjm Posted October 7, 2018 Share Posted October 7, 2018 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? Quote Link to comment Share on other sites More sharing options...
baser-b Posted October 7, 2018 Author Share Posted October 7, 2018 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>."; } } } Quote Link to comment Share on other sites More sharing options...
requinix Posted October 7, 2018 Share Posted October 7, 2018 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>."; } } } Quote Link to comment Share on other sites More sharing options...
ginerjm Posted October 8, 2018 Share Posted October 8, 2018 In case you haven't found the issue yet - you don't have a closing brace for your 'if' - at least not in the correct location. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.