Reputation: 520
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.
Upvotes: 0
Views: 67
Reputation: 1265
Please try this
<form method="post" action="">
<input type="string" name="word">
<input type="submit">
</form>
<?php
function translate($word) {
$dict = array('hello' => 'sawadee khap', 'thanks' => 'kap khum khap', 'sorry' => 'mai pen rai');
if (array_key_exists($word, $dict)) {
echo $dict[$word];
} else {
echo " not in dictionary";
}
}
if (isset($_POST["word"])) {
$word = $_POST["word"];
echo $word . " -> ";
translate($word);
} else {
echo "enter a word";
}
?>
Upvotes: 0
Reputation: 1466
PHP has a function for this, called in_array
. You can do something like this :
$dict = array('hello' => 'sawadee khap','thanks' => 'kap khum khap','sorry' => 'mai pen rai');
if(!in_array($word, array_keys($dict))){
echo '"' . $word . '" not found in the dictionary.';
}else{
echo $dict[$word];
}
Edit: Improved
$dict = array('hello' => 'sawadee khap','thanks' => 'kap khum khap','sorry' => 'mai pen rai');
if(!array_key_exists(strtolower($word), $dict)){
echo '"' . $word . '" not found in the dictionary.';
}else{
echo $dict[$word];
}
Upvotes: 3
Reputation: 73
Your top if statement is missing a brace.
As to your question, I assume that you are wanting to walk through the dictionary and if the word isn't found to echo it. You could use a flag. Set it to something before the for loop and then when you find the word set it to something else. Then check at the end of the for loop eg:
$found = false;
foreach ($dict as $en => $th) {
if ($word == $en) {
$found = true'
echo $th;
break;
}
}
if (!$found) echo $word;''
Upvotes: -1
Reputation: 84
you can use array_key_exist :
$dict = array('hello' => 'sawadee khap','thanks' => 'kap khum khap','sorry' => 'mai pen rai');
if (array_key_exists($word, $dict)) {
//in dictionary
}else{
//not in dictionary
}
Upvotes: 1
Reputation: 33813
If you return a value from the function when a word is found or falsoe otherwise you can do a logical test on the result to display the alternative error message.
<form method="post" action="">
<input type="string" name="word">
<input type="submit">
</form>
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' ){
if ( isset( $_POST["word"] ) ) {
$word = $_POST["word"];
echo $word . " -> ";
function translate( $word=false ){
if( $word ){
$dict = array('hello' => 'sawadee khap','thanks' => 'kap khum khap','sorry' => 'mai pen rai');
foreach ($dict as $en => $th) {
if( $word == $en ) {
return $th;
}
}
}
return false;
}
$result=translate( $word );
echo $result ? $result : 'Sorry, not found!';
} else {
echo "enter a word";
}
}
?>
Upvotes: 1
Reputation: 3236
Change your function code
function translate($word){
$dict = array('hello' => 'sawadee khap','thanks' => 'kap khum khap','sorry' => 'mai pen rai');
if(array_key_exists($word, $dict)){
echo $dict[$word];
}else{
echo 'not in dictionary';
}
}
Upvotes: 2