通过 if 语句 PHP 进行字符串匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5263732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:48:12  来源:igfitidea点击:

string matching via if statement PHP

phpif-statement

提问by RSM

I have an input text area which when filled out and sent, puts whatever was typed in into the variable

我有一个输入文本区域,当填写并发送时,将输入的任何内容放入变量中

$input

This is then put through an if statement to check whether or not its the letter a. If it is then echo - you wrote the letter a, else - you did not write the letter a.

然后通过一个 if 语句来检查它是否是字母 a。如果是 echo - 你写了字母 a,否则 - 你没有写字母 a。

<?php    
 $input = $_POST["textarea"];

    echo $input;
    echo "<br />";

    if($input = "a"){
    echo "You wrote a";
    }else{
    echo "You did not write a";
    }


    ?>

It does work, but in the wrong way. Every letter I type in comes as 'You wrote a'. I only want it to echo this if the user typed a. Otherwise, echo 'You did not write a' .

它确实有效,但方式错误。我输入的每个字母都是“你写了一个”。如果用户键入 a,我只希望它回显这一点。否则, echo 'You did not write a' 。

EDIT: When I try == instead of = it says 'You did not write a' for everything. Even when I type a.

编辑:当我尝试 == 而不是 = 时,它会为所有内容显示“您没有写 a”。即使我输入 a.

EDIT 2: When I try the string compare parameter, It didnt work. Any suggestions where I am going wrong?

编辑 2:当我尝试字符串比较参数时,它不起作用。我哪里出错了有什么建议吗?

FULL SCRIPTS OF BOTH PAGES:

两页的完整脚本:

index.php

索引.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

</head>

<body>
<h1>Fun Translator</h1>

<form method="post" action="query.php">
 <textarea name="textarea" id="textarea">
 </textarea>
 <input type="submit" name="send" value="Translate" />
</form>


</body>
</html>

query.php

查询.php

<?php
$input = $_POST["textarea"];

echo $input;
echo "<br />";

if(strcmp($input,'a')==0){
    echo "You wrote a";
    }else{
    echo "You did not write a";
    }


?>

Solution:

解决方案:

trim()

修剪()

回答by DeaconDesperado

== is the conditional for comparison. = is the assignment operator.

== 是比较的条件。= 是赋值运算符。

if($input == "a"){
    echo "You wrote a";
    }else{
    echo "You did not write a";
    }

You might want to use strcmp as it is binary-safe.

您可能想要使用 strcmp,因为它是二进制安全的。

if(strcmp($input,'a')==0){
    echo "You wrote a";
    }else{
    echo "You did not write a";
    }

http://php.net/manual/en/language.operators.comparison.php

http://php.net/manual/en/language.operators.comparison.php

http://php.net/manual/en/function.strcmp.php

http://php.net/manual/en/function.strcmp.php

EDIT:Taking another stab here - in your code as posted (I copy pasted it) you have whitespace in your textarea by default.

编辑:在这里进行另一次尝试 - 在您发布的代码中(我复制粘贴了它),默认情况下,您的 textarea 中有空格。

Don't put a line break between the and tags. This adds whitespace to the start of 'a'.

不要在 和 标签之间放置换行符。这会在 'a' 的开头添加空格。

Removing this made it work properly in my test.

删除它使其在我的测试中正常工作。

EDIT:As per the accepted answer and reposted code, trim() is in fact the proper function to remove all leading and trailing whitespace.

编辑:根据接受的答案和重新发布的代码,trim() 实际上是删除所有前导和尾随空格的正确函数。

回答by Spudley

Your problem is a confusion between the =and ==operators.

您的问题是===运算符之间的混淆。

If you want to compare two values in PHP, you must use ==(that is, two equal signs together).

如果要在 PHP 中比较两个值,必须使用==(即两个等号放在一起)。

Using a single equal sign on its own will set the value on the right to the value on the left (as per its use in the first line of your code example).

单独使用单个等号会将右侧的值设置为左侧的值(根据它在代码示例的第一行中的使用)。

See this page on the php manual: http://www.php.net/manual/en/language.operators.comparison.php

请参阅 php 手册上的此页面:http: //www.php.net/manual/en/language.operators.comparison.php

Note, there is also a tripple-equal operator ===, which is also used for comparison, where you want to also compare the data type of the two sides. For basic use, the double equal is usually sufficient, but the manual page will give you more info on the difference between the two.

注意,还有一个tripple-equal operator ===,它也是用于比较的,这里你还想比较两边的数据类型。对于基本使用,双等号通常就足够了,但手册页会给你更多关于两者区别的信息。

[EDIT]

[编辑]

Re your edit - All the comments suggesting ==instead of =are right. The single-equal operator is the wrong thing to use. If you're still getting a problem with the double-equal operator, then $inputdoesn't actually equal a.

重新编辑 - 所有建议==而不是的评论=都是正确的。使用单等运算符是错误的。如果您仍然遇到双重相等运算符的问题,那么$input实际上并不等于a

In this case, you may need to debug your input. Assuming your echo $input;line does show a, I'd say the most likely scenario is that you're entering some white space with it, maybe a carriage return or something like that.

在这种情况下,您可能需要调试您的输入。假设您的echo $input;行确实显示了a,我会说最有可能的情况是您用它输入了一些空白,可能是回车或类似的东西。

To prove that, you may want to print it like this:

为了证明这一点,您可能希望像这样打印它:

print "<pre>[".$input."]</pre>";

That will show up any unexpected extra characters you may have entered.

这将显示您可能输入的任何意外的额外字符。

If you're not worried about white space, then use the trim()function to get rid of it.

如果你不担心空白,那么使用trim()函数来摆脱它。

You could also use functions like strlen()to check how long the string is, etc.

您还可以使用函数strlen()来检查字符串的长度等。

[EDIT AGAIN]

[再次编辑]

Okay, looking at your HTML code, I'd say it's definitely going to be the white space issue.

好吧,看看你的 HTML 代码,我会说这肯定是空白问题。

<textarea name="textarea" id="textarea">
</textarea>

This will result in the textarea being created with a line feed as it's default content, because the opening and closing <textarea>tags are on separate lines. If you just type ainto it, what you'll actually submit will be aplus a line feed.

这将导致 textarea 使用换行作为默认内容创建,因为开始和结束<textarea>标记位于不同的行上。如果你只是输入a它,你实际提交的将是a一个换行符。

The best advice is probably to trim()the input before you check it.

最好的建议可能trim()是在您检查输入之前。

回答by dialer

Use operator ==instead of =

使用运算符==代替=

回答by David says reinstate Monica