String matching via if statement

I have a text input area, which when filling and sending puts everything that was entered into a variable

$input

This is then done through an if statement to check if its letter is a. If this is then an echo - you wrote the letter a, otherwise - you did not write the letter a.

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

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

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


    ?>

It works, but not so. Each letter I type comes as "You wrote." I just want this to happen again if the user typed a. Otherwise, the echo "you did not write."

EDIT: When I try to use == instead of =, it says “You didn't write” for everything. Even when I type.

EDIT 2: When I try to use the string comparison option, it does not work. Any suggestions I'm wrong about?

FULL SCRIPTS OF BOTH PAGES:

index.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
$input = $_POST["textarea"];

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

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


?>

Decision:

()

+3
4

= ==.

PHP, == ( ).

, ( ).

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

: ===, , . , .

[EDIT]

Re your edit - , == =, . - . , $input a.

. , echo $input; a, , , , , - .

, :

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

, .

, trim(), .

, strlen(), , ..

[ ]

, HTML-, , .

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

, , <textarea> . a, a .

, , trim(), .

+6

== . = - .

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

, 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/function.strcmp.php

: - , ( ), .

. "a".

.

: , trim() .

+8

Use an operator ==instead=

+2
source

All Articles