PHP 中的 Else 语句


Else Statement in PHP

我正在学习PHP,并编写了一个简单的翻译器。

<!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>

当我输入不在数组中的单词时,如何显示字符串"不在字典中"?我也非常感谢任何关于改进代码的反馈或建议。

PHP 有一个函数,叫做 in_array 。你可以做这样的事情:

$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];
}

编辑:改进

$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];
}

更改函数代码

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

如果在找到单词时从函数返回值,否则可以对结果执行逻辑测试以显示备用错误消息。

<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";
        }
    }
?>
您可以使用

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
}

请尝试这个

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

您的顶部 if 语句缺少大括号。

至于你的问题,我假设你想浏览字典,如果没有找到这个词来呼应它。您可以使用标志。将其设置为for循环之前的内容,然后在找到单词时将其设置为其他内容。然后在 for 循环的末尾检查,例如:

$found = false;
foreach ($dict as $en => $th) {
    if ($word == $en) {
        $found = true'
        echo $th;
        break;
    }
}
if (!$found) echo $word;''