繁体   English   中英

PHP中的其他声明

[英]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> 

输入不在数组中的单词时,如何显示字符串“ not in dictionary”? 对于改进代码的任何反馈或建议,我也将不胜感激。

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

您的top if语句缺少括号。

关于您的问题,我认为您想浏览字典,如果找不到该词来回显它。 您可以使用一个标志。 将其设置为for循环之前的内容,然后在找到单词时将其设置为其他内容。 然后检查for循环的末尾,例如:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM