473,386 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

additional variable breaks script

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>
Nov 5 '09 #1

✓ answered 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.

9 1822
Dormilich
8,658 Expert Mod 8TB
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.
Nov 5 '09 #2
Markus
6,050 Expert 4TB
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.
Nov 5 '09 #3
Dormilich
8,658 Expert Mod 8TB
@Markus
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
Nov 5 '09 #4
Markus
6,050 Expert 4TB
@Dormilich
You know I know that! But in the context, I highly doubt he has these set as constants.
Nov 5 '09 #5
Dormilich
8,658 Expert Mod 8TB
just to have it posted for the next reader...
Nov 5 '09 #6
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. ?>
Nov 10 '09 #7
TheServant
1,168 Expert 1GB
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.
Nov 10 '09 #8
Dormilich
8,658 Expert Mod 8TB
the reasons your script fails has already been mentioned by Markus and me.
Nov 10 '09 #9
thanks, i have resolved the problem mate, much appreciated
Nov 10 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Bryan Ashby | last post by:
All, I'm looking for a way to define a "dummy" variable in JavaScript; specifically for the window object. I would like to define a window object that would normally be generated with...
29
by: Friday | last post by:
Sorry if this is the wrong group. I tried to find the one I thought would be most relevant. I'm an old PHP guy, who knows little about asp and NOTHING about asp.net, but need to learn at least...
5
by: Hemanth | last post by:
Hello there, I'm trying to read an excel worksheet (with more than 5000 rows and 30 columns) using PHP. I'm using the "excelreader" script I found over the web -...
2
by: John R. Lewis | last post by:
I posted this yesterday with a different email address. I am reposting with my fake-address as given to me by Microsoft so that I can be guraranteed a response from a support representative. Sorry...
5
by: brett | last post by:
I'd like to display a string variable on a webpage depending if the URL has "localhost" in it or not. The string variable will have line breaks and this text: <script type="text/javascript"...
13
by: Jake Barnes | last post by:
I saw this sentence: "The last stage of variable instantiation is to create named properties of the Variable object that correspond with all the local variables declared within the function." ...
2
by: ThunStorm | last post by:
I have a script that is working but I can't figure out how to add paragraph breaks into the textarea. Can anyone help me? If you run the script, the problem will be easier to understand. If...
3
by: zjw2112 | last post by:
Hello. I have some javascript code that dynamically creates a textarea and sets the wrap value to hard, which I thought would preserve CR/LF in the textarea: var otherTextArea =...
3
by: Wayne Deleersnyder | last post by:
Hi All, I'm trying to create a function that will cause a pop-up alert to appear if dates which were chosen from a drop-down list were invalid on a page. There's 4 dates, so there's the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.