简体   繁体   English

PHP中的其他声明

[英]Else Statement in PHP

I'm learning PHP and wrote a simple translator. 我正在学习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> 

How can I display the string 'not in dictionary' when I enter a word that isn't in the array? 输入不在数组中的单词时,如何显示字符串“ not in dictionary”? I'd also appreciate any feedback or suggestions on improving the code. 对于改进代码的任何反馈或建议,我也将不胜感激。

PHP has a function for this, called in_array . PHP为此提供了一个函数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];
}

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

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

you can use array_key_exist : 您可以使用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
}

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

Your top if statement is missing a brace. 您的top if语句缺少括号。

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. 将其设置为for循环之前的内容,然后在找到单词时将其设置为其他内容。 Then check at the end of the for loop eg: 然后检查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