I'm learning PHP and wrote a simple translator.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Translator</title>
</head>
<body>
<form method="post" action="">
<input type="string" name="word">
<input type="submit">
</form>
<?php
if (isset($_POST["word"])) {
$word = $_POST["word"];
echo $word . " -> ";
function translate($word){
$dict = array('hello' => 'sawadee khap','thanks' => 'kap khum khap','sorry' => 'mai pen rai');
foreach ($dict as $en => $th) {
if ($word == $en) {
echo $th;
break;
}
}
}
translate($word);
} else {
echo "enter a word";
}
?>
</body>
</html>
How can I display the string 'not in dictionary' when I enter a word that isn't in the array? I'd also appreciate any feedback or suggestions on improving the code.
$dict[$word]
.