Connecting Tech Pros Worldwide Forums | Help | Site Map

additional variable breaks script

Newbie
 
Join Date: Nov 2009
Location: london, england
Posts: 3
#1: 3 Weeks Ago
why wont this script work, i added the operator2 variable an it stopped working
Expand|Select|Wrap|Line Numbers
  1. <html>
  2.  
  3. <head>
  4. <title>A Metric/Imperial Converter</title>
  5. </head>
  6. <body>
  7.  
  8. <?php
  9.  
  10. $number1 = $_POST[number1];
  11. $operator = $_POST[operator];
  12.  
  13. if ($operator == "Miles"){
  14. $answer = $number1*1.609;
  15. $operator2 = "Kilometers";
  16. }
  17.  
  18. elseif ($operator == "Kilometers"){
  19. $answer = $number1/1.609;
  20. $operator2 = "Miles";
  21. }
  22.  
  23. elseif ($operator == "Inches"){
  24. $answer = $number1*2.54;
  25. $operator2 = "Centimetres";
  26. }
  27.  
  28. elseif ($operator == "Centimetres"){
  29. $answer = $number1/2.54;
  30. $operator2 = "Inches";
  31. }
  32.  
  33. elseif ($operator == "Pounds"){
  34. $answer = $number1*0.454;
  35. $operator2 = "Kilograms";
  36. }
  37.  
  38. elseif ($operator == "Kilograms"){
  39. $answer = $number1/0.454;
  40. $operator2 = "Pounds";
  41. }
  42.  
  43. elseif ($operator == "Horsepower"){
  44. $answer = $number1*0.746;
  45. $operator2 = "Kilowatts";
  46. }
  47.  
  48. elseif ($operator == "Kilowatts"){
  49. $answer = $number1/0.746;
  50. $operator2 = "Horsepower";
  51. }
  52.  
  53. elseif ($operator == "Pints"){
  54. $answer = $number1*0.568;
  55. $operator2 = "Litres";
  56. }
  57.  
  58. else {
  59. $answer = $number1/0.568;
  60. $operator = "Litres";
  61. $operator2 = "Pints";
  62. }
  63.  
  64. echo "$number1 $operator is equal to $answer $operator2";
  65.  
  66. ?>
  67.  
  68. <br>
  69.  
  70. <a href="Metricconvert.php/">Click here to do another conversion</a>
  71.  
  72. </body>
  73.  
  74. </html>
best answer - posted by Markus
Please see Turn On PHP Debugging Messages.

Another problem with your code (that will be shown when you turn on error reporting: Array indexes should be wrapped with quotes (single or double). Consider the following:

Expand|Select|Wrap|Line Numbers
  1. // bad
  2. echo $array[some_index];
  3. // good
  4. echo $array['some_index'];
  5.  
Mark.

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,662
#2: 3 Weeks Ago

re: additional variable breaks script


if you don’t submit a form via post method, the $_POST array is empty.

this would have thrown a warning, if error reporting was enabled.
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,949
#3: 3 Weeks Ago

re: additional variable breaks script


Please see Turn On PHP Debugging Messages.

Another problem with your code (that will be shown when you turn on error reporting: Array indexes should be wrapped with quotes (single or double). Consider the following:

Expand|Select|Wrap|Line Numbers
  1. // bad
  2. echo $array[some_index];
  3. // good
  4. echo $array['some_index'];
  5.  
Mark.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,662
#4: 3 Weeks Ago

re: additional variable breaks script


Quote:

Originally Posted by Markus View Post

Another problem with your code : Array indexes should be wrapped with quotes (single or double).

there is only one exception, and it nearly never occurs.
Expand|Select|Wrap|Line Numbers
  1. define("some_index", "some_value");
  2. $array[some_index];
  3. // is now equivalent to
  4. $array["some_value"];
  5.  
  6. // but writing constants in lower case is considered bad practice
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,949
#5: 3 Weeks Ago

re: additional variable breaks script


Quote:

Originally Posted by Dormilich View Post

there is only one exception, and it nearly never occurs.

Expand|Select|Wrap|Line Numbers
  1. define("some_index", "some_value");
  2. $array[some_index];
  3. // is now equivalent to
  4. $array["some_value"];
  5.  
  6. // but writing constants in lower case is considered bad practice

You know I know that! But in the context, I highly doubt he has these set as constants.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,662
#6: 3 Weeks Ago

re: additional variable breaks script


just to have it posted for the next reader...
Newbie
 
Join Date: Nov 2009
Location: london, england
Posts: 3
#7: 2 Weeks Ago

re: additional variable breaks script


Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $number1 = $_POST[number1];
  4. $operator = $_POST[operator];
  5.  
  6. if ($operator == "Miles"){
  7. $answer = $number1*1.609;
  8. $operator2 = "Kilometers";
  9. }
  10.  
  11. elseif ($operator == "Kilometers"){
  12. $answer = $number1/1.609;
  13. $operator2 = "Miles";
  14. }
  15.  
  16. elseif ($operator == "Inches"){
  17. $answer = $number1*2.54;
  18. $operator2 = "Centimetres";
  19. }
  20.  
  21. elseif ($operator == "Centimetres"){
  22. $answer = $number1/2.54;
  23. $operator2 = "Inches";
  24. }
  25.  
  26. elseif ($operator == "Pounds"){
  27. $answer = $number1*0.454;
  28. $operator2 = "Kilograms";
  29. }
  30.  
  31. elseif ($operator == "Kilograms"){
  32. $answer = $number1/0.454;
  33. $operator2 = "Pounds";
  34. }
  35.  
  36. elseif ($operator == "Horsepower"){
  37. $answer = $number1*0.746;
  38. $operator2 = "Kilowatts";
  39. }
  40.  
  41. elseif ($operator == "Kilowatts"){
  42. $answer = $number1/0.746;
  43. $operator2 = "Horsepower";
  44. }
  45.  
  46. elseif ($operator == "Pints"){
  47. $answer = $number1*0.568;
  48. $operator2 = "Litres";
  49. }
  50.  
  51. elseif ( $operator == "litres"){
  52. $answer = $number1/0.568;
  53. $operator2 = "Pints";
  54. }
  55.  
  56. else {
  57. echo "An error has occured please go back and try again";
  58. }
  59.  
  60. echo "$number1 $operator is equal to $answer $operator2";
  61.  
  62. ?>
TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 914
#8: 2 Weeks Ago

re: additional variable breaks script


Welcome to Bytes. Please use [code] tags around your code so that we can read the code easier and assist you a lot faster. Your title tells us that something is not working, but that's about it. You need to give more details. It sounds like all the others are working, and you're not getting the "An error has occured please go back and try again" error, so what error are you getting?

The only difference I have noticed is your litres and Litres difference in the last one.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,662
#9: 2 Weeks Ago

re: additional variable breaks script


the reasons your script fails has already been mentioned by Markus and me.
Newbie
 
Join Date: Nov 2009
Location: london, england
Posts: 3
#10: 2 Weeks Ago

re: additional variable breaks script


thanks, i have resolved the problem mate, much appreciated
Reply