prathameshkakade Posted October 20, 2012 Share Posted October 20, 2012 Hello everyone. I tried to find out wether a number is armstrong or not.My logic was right but few php errors are creeping.I am new to PHP.So please help me out. Here is the code armstrong.php <html> <head> <title> Armstrong number </title> </head> <body> <?php $arm=0; $i; $k; $i=$_POST["i"]; $j=$i; while($j!=0) { $k=$j%10; $arm=$arm+($k*$k*$k); $j=$j/10; } if($i==$arm) {echo "Yes it is an Armstrong number";} else {echo "No,it is not an Armstrong Number";} ?> <form method="POST" action="armstrong.php"> <input type="text" name="i" /> <br> <br> <input type="submit" name="submit" /> <input type="reset" name="reset" /> </form> </body> </html> The error is it shows "Yes it is an Armstrong number" by default when we open armstrong.php in browser.And when we enter a number and click submit,every thing goes fine. Here is a screenshot of it. <hr> And also some php errors are visible(undefined variable.....). Please help. Quote Link to comment https://forums.phpfreaks.com/topic/269713-armstrong-number-in-php/ Share on other sites More sharing options...
Solution kicken Posted October 20, 2012 Solution Share Posted October 20, 2012 You need to make sure that your code only runs if you have submitted a number. You can do that by wrapping the whole block of code in a if() statement that checks whether $_POST['i'] exists or not if (isset($_POST['i'])){ .... } Quote Link to comment https://forums.phpfreaks.com/topic/269713-armstrong-number-in-php/#findComment-1386634 Share on other sites More sharing options...
prathameshkakade Posted October 21, 2012 Author Share Posted October 21, 2012 You need to make sure that your code only runs if you have submitted a number. You can do that by wrapping the whole block of code in a if() statement that checks whether $_POST['i'] exists or not if (isset($_POST['i'])){ .... } Thanks man.It worked out perfectly.But can you tell me why we added that lines to code?I mean the function of (isset($_POST['i'])). Quote Link to comment https://forums.phpfreaks.com/topic/269713-armstrong-number-in-php/#findComment-1386692 Share on other sites More sharing options...
Zane Posted October 21, 2012 Share Posted October 21, 2012 If you read the manual it will all become clear to you http://php.net/isset isset does exactly what it says it does.. check if a variable is set... and returns true or false. Quote Link to comment https://forums.phpfreaks.com/topic/269713-armstrong-number-in-php/#findComment-1386695 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.