This question already has an answer here:
- PHP - String comparison is not working 3 answers
I have written some PHP to convert Fahrenheit to Celsius and vice-versa. I created the following function to validate the data and give an output:
function convert($convertNum, $string){
if (strcmp(strtoupper($string), 'F')) {
$convertedC = ($convertNum * 1.8) + 32;
echo '<h3>' . $convertNum . ' °C is ' . $convertedC . ' °F <br />';
}
else if (strcmp(strtoupper($string), 'C')) {
$convertedF = ($convertNum - 32) * 0.8;
echo '<h3>' . $convertNum . ' °F is ' . $convertedF . ' °C <br />';
}else {
echo "The data you entered is invalid.";
}
}
However for some reason enter a string such as '32' and 'Z' still returns a value
32 °C is 89.6 °F
instead of outputting
The data you entered is invalid.
Aucun commentaire:
Enregistrer un commentaire