Shadowing Posted December 2, 2011 Share Posted December 2, 2011 I'm a little confused exactly on how using this } works going to be hard to explain what im having problems with. I'm using JEdit for my editor and I notice if I click on one of these { it will put a box around where it ends there is 4 } at the bottom of the code by them selfs "on their own lines" and 2 in the middle by them selfs "on their own lines how does the code know which ones belong to which { appreicate any help in helping me understand this thanks this code is from a tutor online <? include_once("connect.php"); ?> <html> <body> <?php if(isset($_POST['Login'])) { if(!preg_match('/^[A-Za-z0-9]{5,20}$/',$_POST['Username'])){ // before we fetch anything from the database we want to see if the user name is in the correct format. echo "Invalid Username."; }else{ $query = "SELECT password,id,login_ip FROM users WHERE name='".mysql_real_escape_string($_POST['Username'])."'"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($result); // Search the database and get the password, id, and login ip that belongs to the name in the username field. if(empty($row['id'])){ // check if the id exist and it isn't blank. echo "Account doesn't exist."; }else{ if(md5($_POST['password']) != $row['password']){ // if the account does exist this is matching the password with the password typed in the password field. notice to read the md5 hash we need to use the md5 function. echo "Your password is incorrect."; }else{ if(empty($row['login_ip'])){ // checks to see if the login ip has an ip already $row['login_ip'] = $_SERVER['REMOTE_ADDR']; }else{ $ip_information = explode("-", $row['login_ip']); // if the ip is different from the ip that is on the database it will store it if (in_array($_SERVER['REMOTE_ADDR'], $ip_information)) { $row['login_ip'] = $row['login_ip']; }else{ $row['login_ip'] = $row['login_ip']."-".$_SERVER['REMOTE_ADDR']; } } $_SESSION['user_id'] = $row['id'];// this line of code is very important. This saves the user id in the php session so we can use it in the game to display information to the user. $result = mysql_query("UPDATE users SET userip='".mysql_real_escape_string($_SERVER['REMOTE_ADDR'])."',login_ip='".mysql_real_escape_string($row['login_ip'])."' WHERE id='".mysql_real_escape_string($_SESSION['user_id'])."'") or die(mysql_error()); // to test that the session saves well we are using the sessions id update the database with the ip information we have received. header("Location: Sample.php"); // this header redirects me to the Sample.php i made earlier } } } } ?> <form id="form1" name="form1" method="post" action=""><center> GAME LOGIN <br /> <br /> Username: <input type="text" name="Username" id="Username" /> <br /> <br /> Password: <input type="password" name="password" id="password" /> <br /> <br /> <input type="submit" name="Login" id="Login" value="Login" /> </center> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/252290-how-does-this-symbol-work/ Share on other sites More sharing options...
Glese Posted December 2, 2011 Share Posted December 2, 2011 It describes a section (or code block) in which code becomes executed. Imagine the execution of a PHP file going down linear from top to bottom, line by line by line - line 1, line 2, line 3 and so on. Since PHP is a loosely written programming language, which means that the position of the code are not hard coded into the language, the interpretation of the file needs to know where a code block starts and where it ends, thus this, which I called a section, describes as follows: if (isset($variable)) { // if condition met, execute code } a code block, which is in this example an if statement, which contains code that needs to be executed as soon as the condition is met. At the same time the interpretation needs to know where the container of the code block exactly ends and a new code block starts, and that is what the curly braces are for. Quote Link to comment https://forums.phpfreaks.com/topic/252290-how-does-this-symbol-work/#findComment-1293401 Share on other sites More sharing options...
Shadowing Posted December 2, 2011 Author Share Posted December 2, 2011 Thanks for the reply The part that I dont understand or cant wrap my head around is that how does the script know which } to what statement it belongs too? Notice at the end of my code it has several }. what really confuses me the most is the two } above this line $_SESSION['user_id'] = $row['id'];// this line of code is very important. This saves the user id in the php session so we can use it in the game to display information to the user. Jedit is telling me those two } goes with the statements just above it but how does the code know its not for the statements at the begining of the script that ends at the end of the script. This is really hard to explain on what is confusing me hope i explained it. I really need help understanding this cause its like the only thing left that really has me confused on PHP Quote Link to comment https://forums.phpfreaks.com/topic/252290-how-does-this-symbol-work/#findComment-1293627 Share on other sites More sharing options...
Drongo_III Posted December 2, 2011 Share Posted December 2, 2011 Well I think when its parsed the system essentially looks for an opening { and if no other { is found then the next } is considered the closing one. Whereas is two { { are found then it will look for the next }} to close. That make more sense hehe? Thanks for the reply The part that I dont understand or cant wrap my head around is that how does the script know which } to what statement it belongs too? Notice at the end of my code it has several }. what really confuses me the most is the two } above this line $_SESSION['user_id'] = $row['id'];// this line of code is very important. This saves the user id in the php session so we can use it in the game to display information to the user. Jedit is telling me those two } goes with the statements just above it but how does the code know its not for the statements at the begining of the script that ends at the end of the script. This is really hard to explain on what is confusing me hope i explained it. I really need help understanding this cause its like the only thing left that really has me confused on PHP Quote Link to comment https://forums.phpfreaks.com/topic/252290-how-does-this-symbol-work/#findComment-1293629 Share on other sites More sharing options...
Glese Posted December 3, 2011 Share Posted December 3, 2011 shadowing, do imagine those two { } as a container, one if statement is containing another: if { if { } else { { } Also the imagine the interpretation going form one line to the next from top to bottom. Quote Link to comment https://forums.phpfreaks.com/topic/252290-how-does-this-symbol-work/#findComment-1293830 Share on other sites More sharing options...
Shadowing Posted December 3, 2011 Author Share Posted December 3, 2011 so thats why the First If statment has to have a } at the very end of the script so it contains all the other if statments? the first if statment always contains all the other if statments in a script? then teh 2nd if statement contains all the if statments besides the first if statment? I really appreciate the help in getting me to try to understand this Quote Link to comment https://forums.phpfreaks.com/topic/252290-how-does-this-symbol-work/#findComment-1293836 Share on other sites More sharing options...
Shadowing Posted December 3, 2011 Author Share Posted December 3, 2011 Ok im starting to understand this I have another question. Im using Jedit for my editor which shows me exactly what you are saying how it works. not sure if you guys use a editor or not but why does my eidtor say on the last If statment of this script that the 3rd to last if statment doesnt try to enclose the last if statement if that sounded confusing here is another way of saying what im saying lol why doesnt the 3rd from last if statment enclose the last if statement? is it just simply cause the two }} are simply in the wrong place and should be at the end of the script? Quote Link to comment https://forums.phpfreaks.com/topic/252290-how-does-this-symbol-work/#findComment-1293838 Share on other sites More sharing options...
Shadowing Posted December 3, 2011 Author Share Posted December 3, 2011 hmm if I move those two } on the other side of the last IF statment the script stops working says error on line 89 so now my other thoughts is the last if statment gets ignored for some reason. cause of some command before the last if statment seperating it from the other if statments? then if thats true then why is all the }}}}} after the last if statment when it should be just before it. oh boy lol I know im asking alot here. if one of you guys could please load this code into your editor and see what im talking about. Cause i really need to understand this Quote Link to comment https://forums.phpfreaks.com/topic/252290-how-does-this-symbol-work/#findComment-1293840 Share on other sites More sharing options...
Shadowing Posted December 3, 2011 Author Share Posted December 3, 2011 Ahh i did the test wrong haha. it all works now exactly how you described I got rid of the two }} and put them at the bottom with the rest and now it all ifs are inside each other from first to last. and the script works. Whats even better now something on the script works that didnt before Thanks alot for the help im so glad I understand how that works now. It was driving me crazy haha Quote Link to comment https://forums.phpfreaks.com/topic/252290-how-does-this-symbol-work/#findComment-1293859 Share on other sites More sharing options...
Glese Posted December 3, 2011 Share Posted December 3, 2011 shadowing, do make use of proper indentation as well, it will help you see it clearer and improve your thinking ways. Try to write scarce, proper and elegant code. You try to be organized as I can tell in your first code example, but you can improve it even more by making it more systematic and sense making, and by also using bigger gaps between separate parts, and by aligning the same together. I also would advice to place the commenting above the block with an empty line in between, I also recommend to write the commenting in proper grammar and full sentences. That way you will have a clean, systematic and sense making code, and the language itself will make more sense to you as well. Also, do not worry, about thinking through the bare basics too much, in the end it will pay off and will make you become a thorough understander. Quote Link to comment https://forums.phpfreaks.com/topic/252290-how-does-this-symbol-work/#findComment-1293869 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.