0

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.

2
  • 2
    let the golf code begin!
    – Kevin
    Commented Feb 4, 2016 at 6:49
  • You don't really need a loop for what you're doing. You can just echo the EN to TH translation by doing $dict[$word]. Commented Feb 4, 2016 at 6:49

6 Answers 6

3

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];
}
3
  • } else { echo $dict[$word]; } would make it complete
    – Rene Korss
    Commented Feb 4, 2016 at 6:47
  • @ReneKorss Thanks. Edited. Commented Feb 4, 2016 at 6:48
  • might as well add an strtolower on top of that, it would be weird not to match as simple Hello (uppercase)
    – Kevin
    Commented Feb 4, 2016 at 6:52
2

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';
}
} 
1

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";
        }
    }
?>
1

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
}
0

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";
}
?>
1
  • @ReneKorss Because we CAN! :D Commented Feb 4, 2016 at 6:52
-1

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;''

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.