Reputation: 15128
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.
<?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' .
EDIT: When I try == instead of = it says 'You did not write a' for everything. Even when I type a.
EDIT 2: When I try the string compare parameter, It didnt work. Any suggestions where I am going wrong?
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";
}
?>
Solution:
trim()
Upvotes: 4
Views: 90936
Reputation: 10267
== 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.
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
EDIT: Taking another stab here - in your code as posted (I copy pasted it) you have whitespace in your textarea by default.
Don't put a line break between the and tags. This adds whitespace to the start of '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.
Upvotes: 13
Reputation: 168853
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).
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
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.
[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 $input
doesn't actually equal 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.
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.
You could also use functions like strlen()
to check how long the string is, etc.
[EDIT AGAIN]
Okay, looking at your HTML code, I'd say it's definitely going to be the white space issue.
<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 a
into it, what you'll actually submit will be a
plus a line feed.
The best advice is probably to trim()
the input before you check it.
Upvotes: 9