Two variables are 100% the same, yet when compared in an if statement, nothing is executed
I have two strings. They are both ver开发者_如何学JAVAy long, but I have used echo
to display both of them prior. Not a single character is different. But when I put them in the if
statement, nothing inside executes.
if ($arrayFromMySqliArrayFetch['code'] == $_POST['code']) { echo 'please work.'; }
Is something just flying over my head? I don't understand why nothing will get executed?
I have duplicated the if
statement but with different internals. How would I even narrow down what is causing the problem if there shouldn't be one? :(
Edit
Thanks for everyone's help. FYI, the strings are both trimmed and all. Using var_dump
I found out that one strings has 128 characters (what it should be), and one, the $_POST
has 132. Any ideas?
use var_dump() to better understand type of each variable.
What happens if you try
if ('same' == 'same') { - does it execute? I'm guessing so?
Try trimming your values if (trim($val) == trim($val)) to make sure there is no whitespace too.
You should use the strcmp()
function to compare two strings, or even the === operator. See this link for more about string comparison in PHP.
Try to trim()
the strings and then compare:
if (trim($arrayFromMySqliArrayFetch['code']) == trim($_POST['code']))
echo 'please work.';
Sometimes due to leading or trailing spaces (which are not readily visible) this occurs.
One of the randomly generated characters was an ampersand. It caused the difference in string length. I forgot exactly why, but that was what was causing the problem.
精彩评论