December 7, 201114 yr Are these two functions identical or is one more secure/reliable that the other... if (strcmp($string1,$string2) === '0') { echo 'match'; } else { echo 'no match'; } and if ($string1 === $string2) { echo 'match'; } else { echo 'no match'; }
December 7, 201114 yr Well first and foremost, your original code will never output "match". strcmp returns an integer, so '0' will never match when the strings do. You need to remove the quotes from around it As for them being identical, yes they will work just the same if you make my edit above. Note that you also don't need the third = in your first example either, since it's always going to return an integer Edited December 7, 201114 yr by Jay Gilford
December 7, 201114 yr If you're wanting to compare them and work out if they're identical to each other yes. strcmp does more than just compare if they are identical, it actually tests if the first string is a higher/lower value to the second string
December 7, 201114 yr Author I actually just did a test and there is a slight difference... If $string1 = false and $string 2 = "" if ($string1 === $string2) { echo 'match'; } else { echo 'no match'; } echo '<br />'; if (strcmp($string1,$string2) === 0) { echo 'match'; } else { echo 'no match'; } The first example outputs 'no match' while the second example outputs 'match', that is with the second example being === or ==
December 7, 201114 yr Author If you're wanting to compare them and work out if they're identical to each other yes. strcmp does more than just compare if they are identical, it actually tests if the first string is a higher/lower value to the second string Ok that's cleared my question up, I read the php manual but it didn't say anything about using the first example compared to strcmp.
December 7, 201114 yr Why are you using a boolean value for a string? strcmp is expecting two string input. it will assume the false as an empty string Edited December 7, 201114 yr by Jay Gilford
December 7, 201114 yr Author Why are you using a boolean value for a string? strcmp is expecting two string input. it will assume the false as an empty string I was just checking various combinations to see if there were any differences.
Create an account or sign in to comment