Carl Skeel Posted September 24, 2012 Share Posted September 24, 2012 (edited) Write a script that uses a conditional operator to determine whether a variable contains a number and whether the number is even. You need to use the is_numeric() function and the conditional operator. For floating-point numbers, you need to use the round() function to convert the value to the nearest whole number. Save the document as IsEven.php. Reinforcement Exercises1-6 This has been a hard to find solution and I am submitting mine. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[url="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd%22>"]http://www.w3.org/TR...l1-strict.dtd">[/url] <html xmlns="[url="http://www.w3.org/1999/xhtml%22>"]http://www.w3.org/1999/xhtml">[/url] <head> <title>PHPIsEven1-6</title> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> </head> <body> <form action="isEven.php" method="post"> Input Number:<input type="text" size="36" maxlength="36" name="Fnumber">:<br /> <input type="submit" value="submit" name="submit"><br /> </form><br /> <?php // Carl Skeel # 2012/09/24 $Fnumber = $_POST['Fnumber']; $Number = $Fnumber; if (is_numeric($Number)){ $Result = ""; if( $Number & 1 ){ $Result = "The number (" . round($Number) . ") and is numeric and it's odd!"; }else{ $Result = "The number (" . round($Number) . ") and is numeric and it's even!"; } }else{ $Result = "($Number) is not numeric!"; } echo $Result; ?> </body> </html> I hope this helps anyone on this one! IsEven.php Edited September 24, 2012 by Zane Please use code tags Quote Link to comment https://forums.phpfreaks.com/topic/268718-conditional-operator/ Share on other sites More sharing options...
Carl Skeel Posted September 24, 2012 Author Share Posted September 24, 2012 (edited) Also this file must be server ran such as with wamp server to test it. Edited September 24, 2012 by Zane Quote Link to comment https://forums.phpfreaks.com/topic/268718-conditional-operator/#findComment-1380431 Share on other sites More sharing options...
Zane Posted September 24, 2012 Share Posted September 24, 2012 This is not a question, Quote Link to comment https://forums.phpfreaks.com/topic/268718-conditional-operator/#findComment-1380437 Share on other sites More sharing options...
Carl Skeel Posted September 24, 2012 Author Share Posted September 24, 2012 Also this file must be server ran such as with wamp server to test it. OK my question is the file gets an err which I would like to know the solution but it runs fine as long as you input a value! Quote Link to comment https://forums.phpfreaks.com/topic/268718-conditional-operator/#findComment-1380439 Share on other sites More sharing options...
Zane Posted September 24, 2012 Share Posted September 24, 2012 What err? You should have mentioned that to begin with Quote Link to comment https://forums.phpfreaks.com/topic/268718-conditional-operator/#findComment-1380441 Share on other sites More sharing options...
Carl Skeel Posted September 24, 2012 Author Share Posted September 24, 2012 this is the image of the error you get but if you submit a value then it works fine can anyone help with this error? Quote Link to comment https://forums.phpfreaks.com/topic/268718-conditional-operator/#findComment-1380443 Share on other sites More sharing options...
Zane Posted September 24, 2012 Share Posted September 24, 2012 You have to use PHP´s isset function to check whether or not the variable exists or not... That is why you get an undefined index...you haven´t hit the submit button therefore POST is empty. $Number = isset($_POST['Fnumber']) ? $_POST['Fnumber'] : null; Quote Link to comment https://forums.phpfreaks.com/topic/268718-conditional-operator/#findComment-1380448 Share on other sites More sharing options...
Jessica Posted September 24, 2012 Share Posted September 24, 2012 Also, in the future please pay attention to the rules. This site is not for homework. Quote Link to comment https://forums.phpfreaks.com/topic/268718-conditional-operator/#findComment-1380572 Share on other sites More sharing options...
Carl Skeel Posted September 26, 2012 Author Share Posted September 26, 2012 Sorry Jessica and Zane I appreciated the help and the only thing the text was asking for was the general if statement but I had went way beyond that with creating the form and all and had only asked for help with the one little err I had even tho it worked and what seems so minor to you all was a big help to me then after Zane's help I found I needed to add another If arround the whole thing otherwise it kicked out the echo this is not numeric just by running it but after looking it over I figured out the rest after Zane's help with the isset so again thank you Zane for your time and help. Quote Link to comment https://forums.phpfreaks.com/topic/268718-conditional-operator/#findComment-1380970 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.