SEARCH

The How-To Geek Forums Have Migrated to Discourse

How-To Geek Forums / Geek Stuff

PHP compare variable to string

(2 posts)
  • Started 6 years ago by jmcguire
  • Latest reply from jd2066
  • Topic Viewed 9705 times

jmcguire
jmcguire
Posts: 552

I have retrieved information using IMAP and stored it in a variable.

I know what the information is, because I have an echo printed to the screen.

When I try to execute code using an if statement comparing the content, it doesn't work.

if ($var = "hello")
{
echo "example";
}

The information is retrieved from the body of an e-mail. For example, if the entire body was "hello" and my code read as it does above, "example" never gets echoed to the screen, which of course means that the if statement was false.

Does anyone have any clue as to what I need to do?

***UPDATE***

I have been searching Google, and just a few minutes after I submitted this, I found the solution.

if (strpos($var, "hello")

Sorry if I have wasted anyone's time.

Posted 6 years ago
Top
 
jd2066
jd2066
Posts: 3814

This problem has happened to me before too.
You need to use two equal signs instead of one.
The equal sign even within an if statement means to set the variable to the text.
Two equal signs are for comparing.
So if ($var = "hello") { echo "text"; } means Set $var to "hello"
And if ($var == "hello") { echo "text"; } means When $var and "hello" are the same echo "text"
I have no idea why it works like that in php but it does.

Edit: It appears you found another way that works too.

Posted 6 years ago
Top
 



Topic Closed

This topic has been closed to new replies.

Get Free Articles in Your Inbox!

Join 134,000 newsletter readers

Email:

Go check your email!