isuckat_php Posted April 3, 2010 Share Posted April 3, 2010 I have this calculator code which i recieved assitance from phpfreaks.com to complete. Once again help will be greatly appreciated. I can read majority of the code although i missed alot of ";" and "(" and {" which is frustrating. For example: elseif ((!ereg("[0-9]",$score_1)) || (!ereg("[0-9]",$score_2) || (!ereg("[0-9]",$score_3)))) There are alot of ")" at the end of that... just doesn't make sense to me. A more important matter is that I am having trouble reading this part of the code: if (isset($_POST['submit'])) { $score_1 = (isset($_POST['score_1'])) ? $_POST['score_1'] : ''; $score_2 = (isset($_POST['score_2'])) ? $_POST['score_2'] : ''; $score_3 = (isset($_POST['score_3'])) ? $_POST['score_3'] : ''; I see that the more variables are being assigned; however i do not understand what it is being assigned to and how it works. I went with this and it works great with my code, no complaints I am just unable to read this part of the code. Here is the complete code: <html> <body bgcolor="#ffff33"> <?php if (isset($_POST['submit'])) { $score_1 = (isset($_POST['score_1'])) ? $_POST['score_1'] : ''; $score_2 = (isset($_POST['score_2'])) ? $_POST['score_2'] : ''; $score_3 = (isset($_POST['score_3'])) ? $_POST['score_3'] : ''; if (($score_1 == "") || ($score_2 =="") || ($score_3 =="")) { echo("<font face=\"Tahoma\" size=\"2\" color=\"#FF0000\"><b>Go play some more freegumph! Need at least three scores to calculate a handicap.</b></font><br>"); } elseif ((!ereg("[0-9]",$score_1)) || (!ereg("[0-9]",$score_2) || (!ereg("[0-9]",$score_3)))) { echo("<font face=\"Tahoma\" size=\"2\" color=\"#FF0000\"><b>You must be a new player. Freegumph scores only consist of numeric values.</b></font><br>"); } elseif (($score_1 < 0) || ($score_2 < 0) || ($score_3 < 0)) { echo("<font face=\"Tahoma\" size=\"2\" color=\"#006600\"><b>Error! Perhaps the lowest possible score is 0.</b></font><br>"); } else { $avg=($score_1 + $score_2 + $score_3)/3; if ($avg >= 27) { $total = 0; } else { $total = ((27- $avg) * (7/10)); } print(" The freegumpher handicap is: ". $total); } } ?> <form method="post" action=""> Enter your three most <i>recent</i> Freegumpher scores to calculate Handicap:<br/> <br/> <input type="text" name="score_1" size=8 <input type="text" name="score_2" size=8 <input type="text" name="score_3" size=8 /><br /><br/> <input type="submit" name="submit" value="Calculate Handicap"/> </form> </body> </html> Once again help is greatly appreciated and thank you for taking the time to read this. Quote Link to comment https://forums.phpfreaks.com/topic/197505-having-trouble-reading-php-code/ Share on other sites More sharing options...
Jax2 Posted April 3, 2010 Share Posted April 3, 2010 As far as the open and closing brackets and parenthesis, just keep in mind that for every one that is opened, there has to be one that closes. You have 4 at the end of the first bit you showed, which is correct. Count on your fingers, add one for each opening, and subtract one for each closing, you'll find out it's perfectly correct. Quote Link to comment https://forums.phpfreaks.com/topic/197505-having-trouble-reading-php-code/#findComment-1036594 Share on other sites More sharing options...
jcbones Posted April 4, 2010 Share Posted April 4, 2010 I added some comments to help you out. Moreover, if you aren't using a good text editor: I suggest Notepad++. It will highlight the syntax for you and help with brackets, and parenth. <?php //Check to make sure the submit button was clicked. if (isset($_POST['submit'])) { //boolean logic (0/false or 1/true) //reads as $this_variable = (this_condition) ? is_true : is_false; //Same as: //if(isset($_POST['score_1'])) { $score_1 = $_POST['score_1']; } //else { $score_1 = ''; } $score_1 = (isset($_POST['score_1'])) ? $_POST['score_1'] : ''; $score_2 = (isset($_POST['score_2'])) ? $_POST['score_2'] : ''; $score_3 = (isset($_POST['score_3'])) ? $_POST['score_3'] : ''; //if scores are all empty return this clause; if (($score_1 == "") || ($score_2 =="") || ($score_3 =="")) { echo("<font face=\"Tahoma\" size=\"2\" color=\"#FF0000\"><b>Go play some more freegumph! Need at least three scores to calculate a handicap.</b></font><br>"); } //A if/else clause conditions are placed in parenth. You can also group things inside of //the clause condition with parenth. //If($user == 'Sam' || ($role == 'admin' && $banned != 1)) = if the user's name is sam, or they are and admin that isn't banned. //I spaced out the next line, so you can see how the parenth. match up. elseif ( ( !ereg("[0-9]",$score_1) ) || ( !ereg("[0-9]",$score_2) ) || ( !ereg("[0-9]",$score_3) ) ) { echo("<font face=\"Tahoma\" size=\"2\" color=\"#FF0000\"><b>You must be a new player. Freegumph scores only consist of numeric values.</b></font><br>"); } elseif (($score_1 < 0) || ($score_2 < 0) || ($score_3 < 0)) { echo("<font face=\"Tahoma\" size=\"2\" color=\"#006600\"><b>Error! Perhaps the lowest possible score is 0.</b></font><br>"); } else { $avg=($score_1 + $score_2 + $score_3)/3; if ($avg >= 27) { $total = 0; } else { $total = ((27- $avg) * (7/10)); } print(" The freegumpher handicap is: ". $total); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/197505-having-trouble-reading-php-code/#findComment-1036625 Share on other sites More sharing options...
jmajeremy Posted April 4, 2010 Share Posted April 4, 2010 In case you missed it on Yahoo! Answers, here's the contents of my response to your script: Never ever ever ever ever use the ereg() function! It is deprecated! Read the http://php.net/ereg about it, which includes alternative functions. preg_match() is what you should be using. The regex your wrote will only match strings that contain a single digit between 0 and 9, by the way. On another topic, your HTML is of very poor quality. Why do you insist on using deprecated functions of languages?? Nobody ever should ever ever use the <font> tag for any purpose whatsoever. If you want to style a page, it should be done in the form of CSS. There's no reason to write out your little <font Tahoma #8888> tag every time. Just write a CSS block for a certain ID, and surround all your text with a <div>. Plus, your HTML isn't even consistently deprecated! Despite your use of outdated HTML, you then go on to misuse XHTML! Your use of self-closing tags (<input />) is a feature of XHTML, not used in HTML. But even if you wanted to use XHTML, your tags are mal-formed. You should always leave a space before the forward slash in a self-closing tag. Valid HTML: <input> Valid XHTML: <input /> or <input></input> Invalid: <input/> Yahoo! Answers Quote Link to comment https://forums.phpfreaks.com/topic/197505-having-trouble-reading-php-code/#findComment-1036635 Share on other sites More sharing options...
ignace Posted April 4, 2010 Share Posted April 4, 2010 Count on your fingers, add one for each opening, and subtract one for each closing, you'll find out it's perfectly correct. Using a little trick you can have up to 3^10 (=59049) possible combinations just by using 2 hands 3^5 (=243) if you only use one. The values are indicated by holding your finger down (0), half-up (1), full-up (2) Quote Link to comment https://forums.phpfreaks.com/topic/197505-having-trouble-reading-php-code/#findComment-1036679 Share on other sites More sharing options...
greatstar00 Posted April 4, 2010 Share Posted April 4, 2010 ignace, i bet you will make him confuse more Quote Link to comment https://forums.phpfreaks.com/topic/197505-having-trouble-reading-php-code/#findComment-1036683 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.