additional variable breaks script | Newbie | | Join Date: Nov 2009 Location: london, england
Posts: 3
| |
why wont this script work, i added the operator2 variable an it stopped working - <html>
-
-
<head>
-
<title>A Metric/Imperial Converter</title>
-
</head>
-
<body>
-
-
<?php
-
-
$number1 = $_POST[number1];
-
$operator = $_POST[operator];
-
-
if ($operator == "Miles"){
-
$answer = $number1*1.609;
-
$operator2 = "Kilometers";
-
}
-
-
elseif ($operator == "Kilometers"){
-
$answer = $number1/1.609;
-
$operator2 = "Miles";
-
}
-
-
elseif ($operator == "Inches"){
-
$answer = $number1*2.54;
-
$operator2 = "Centimetres";
-
}
-
-
elseif ($operator == "Centimetres"){
-
$answer = $number1/2.54;
-
$operator2 = "Inches";
-
}
-
-
elseif ($operator == "Pounds"){
-
$answer = $number1*0.454;
-
$operator2 = "Kilograms";
-
}
-
-
elseif ($operator == "Kilograms"){
-
$answer = $number1/0.454;
-
$operator2 = "Pounds";
-
}
-
-
elseif ($operator == "Horsepower"){
-
$answer = $number1*0.746;
-
$operator2 = "Kilowatts";
-
}
-
-
elseif ($operator == "Kilowatts"){
-
$answer = $number1/0.746;
-
$operator2 = "Horsepower";
-
}
-
-
elseif ($operator == "Pints"){
-
$answer = $number1*0.568;
-
$operator2 = "Litres";
-
}
-
-
else {
-
$answer = $number1/0.568;
-
$operator = "Litres";
-
$operator2 = "Pints";
-
}
-
-
echo "$number1 $operator is equal to $answer $operator2";
-
-
?>
-
-
<br>
-
-
<a href="Metricconvert.php/">Click here to do another conversion</a>
-
-
</body>
-
-
</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: -
// bad
-
echo $array[some_index];
-
// good
-
echo $array['some_index'];
-
Mark.
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,662
| | | 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.
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,949
| | | 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: -
// bad
-
echo $array[some_index];
-
// good
-
echo $array['some_index'];
-
Mark.
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,662
| | | re: additional variable breaks script Quote:
Originally Posted by Markus 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. - define("some_index", "some_value");
-
$array[some_index];
-
// is now equivalent to
-
$array["some_value"];
-
-
// but writing constants in lower case is considered bad practice
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,949
| | | re: additional variable breaks script Quote:
Originally Posted by Dormilich there is only one exception, and it nearly never occurs. - define("some_index", "some_value");
-
$array[some_index];
-
// is now equivalent to
-
$array["some_value"];
-
-
// 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.
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,662
| | | re: additional variable breaks script
just to have it posted for the next reader...
| | Newbie | | Join Date: Nov 2009 Location: london, england
Posts: 3
| | | re: additional variable breaks script - <?php
-
-
$number1 = $_POST[number1];
-
$operator = $_POST[operator];
-
-
if ($operator == "Miles"){
-
$answer = $number1*1.609;
-
$operator2 = "Kilometers";
-
}
-
-
elseif ($operator == "Kilometers"){
-
$answer = $number1/1.609;
-
$operator2 = "Miles";
-
}
-
-
elseif ($operator == "Inches"){
-
$answer = $number1*2.54;
-
$operator2 = "Centimetres";
-
}
-
-
elseif ($operator == "Centimetres"){
-
$answer = $number1/2.54;
-
$operator2 = "Inches";
-
}
-
-
elseif ($operator == "Pounds"){
-
$answer = $number1*0.454;
-
$operator2 = "Kilograms";
-
}
-
-
elseif ($operator == "Kilograms"){
-
$answer = $number1/0.454;
-
$operator2 = "Pounds";
-
}
-
-
elseif ($operator == "Horsepower"){
-
$answer = $number1*0.746;
-
$operator2 = "Kilowatts";
-
}
-
-
elseif ($operator == "Kilowatts"){
-
$answer = $number1/0.746;
-
$operator2 = "Horsepower";
-
}
-
-
elseif ($operator == "Pints"){
-
$answer = $number1*0.568;
-
$operator2 = "Litres";
-
}
-
-
elseif ( $operator == "litres"){
-
$answer = $number1/0.568;
-
$operator2 = "Pints";
-
}
-
-
else {
-
echo "An error has occured please go back and try again";
-
}
-
-
echo "$number1 $operator is equal to $answer $operator2";
-
-
?>
|  | Expert | | Join Date: Feb 2008 Location: Australia
Posts: 914
| | | 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.
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,662
| | | 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
| | | re: additional variable breaks script
thanks, i have resolved the problem mate, much appreciated
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|