spires Posted May 18, 2007 Share Posted May 18, 2007 HI. Can anyone tell how i go about comparing three strings to see if they are the same? I'm building an Email list system, where users can upload three of their friends Emails. I want to check, to make sure they don't load up the same Email three times. I was thinking something like this, But strcmp only compares two strings. I need to compare three strcmp (string $email1, string $email2, string $email3) Any help would be great Thanks Link to comment https://forums.phpfreaks.com/topic/52014-how-to-compare-three-strings/ Share on other sites More sharing options...
hitman6003 Posted May 18, 2007 Share Posted May 18, 2007 if ($email1 == $email2 == $email3) { //do something } Link to comment https://forums.phpfreaks.com/topic/52014-how-to-compare-three-strings/#findComment-256333 Share on other sites More sharing options...
Orio Posted May 18, 2007 Share Posted May 18, 2007 That won't work hitman. <?php if($str1 == $str2 && str2 == $str3) { //They are the same } ?> Orio. Link to comment https://forums.phpfreaks.com/topic/52014-how-to-compare-three-strings/#findComment-256338 Share on other sites More sharing options...
spires Posted May 18, 2007 Author Share Posted May 18, 2007 Cheers guys, Thats great. I knew it had to be somthing like that. Thanks once again. Link to comment https://forums.phpfreaks.com/topic/52014-how-to-compare-three-strings/#findComment-256341 Share on other sites More sharing options...
clown[NOR] Posted May 18, 2007 Share Posted May 18, 2007 i'm not sure... but maybe if you make your own function (ex: compEmail($strin1,$string2,$string3)) <?php function compEmail($email1,$email2,$email3) { if ($email1 == $email2 || $email1 == $email2) { return false; } if ($email2 == $email1 || $email2 == $email3) { return false; } if ($email3 == $email1 || $email3 == $email2) { return false; } else { return true; } } ?> then you can use this: <?php $compEmail = compEmail($email1,$email2,$email3); if ($compEmail) { // it's ok, continue the script } else { // one or more emails were the same } ?> be aware... it's not tested.. but it should get you started... Link to comment https://forums.phpfreaks.com/topic/52014-how-to-compare-three-strings/#findComment-256343 Share on other sites More sharing options...
Barand Posted May 18, 2007 Share Posted May 18, 2007 ~clown If a==b it follows that b==a, so you only need 3 tests for duplicates, not 6. if ((a==b) || (b==c) || (a==c)) return false And what if the user only wants to email a single friend, so b and c are both blank? Link to comment https://forums.phpfreaks.com/topic/52014-how-to-compare-three-strings/#findComment-256353 Share on other sites More sharing options...
clown[NOR] Posted May 18, 2007 Share Posted May 18, 2007 well... before comparing you must rule out any blank fields... if (!empty($email1) && !empty($email2) && !empty($email3)) { compare them all } etc but about the ((a==b) || (b==c) || (a==c)) ... thanks for clearing that up for me... looks like i need to rewrite some codes myself Link to comment https://forums.phpfreaks.com/topic/52014-how-to-compare-three-strings/#findComment-256407 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.