11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

www.it-ebooks.infoCHAPTER 9 • STRINGS AND REGULAR EXPRESSIONSint strcmp(string str1, string str2)It will return one of three possible values based on the comparison outcome:• 0 if str1 and str2 are equal• -1 if str1 is less than str2• 1 if str2 is less than str1Web sites often require a registering user to enter and then confirm a password, lessening thepossibility of an incorrectly entered password as a result of a typing error. strcmp() is a great function forcomparing the two password entries because passwords are usually treated in a case sensitive fashion:if (strcmp($pswd, $pswd2) != 0) {echo "Passwords do not match!";} else {echo "Passwords match!";}Note that the strings must match exactly for strcmp() to consider them equal. For example,Supersecret is different from supersecret. If you’re looking to compare two strings case insensitively,consider strcasecmp(), introduced next.Another common point of confusion regarding this function surrounds its behavior of returning 0 ifthe two strings are equal. This is different from executing a string comparison using the == operator, likeso:if ($str1 == $str2)While both accomplish the same goal, which is to compare two strings, keep in mind that the valuesthey return in doing so are different.Comparing Two Strings Case InsensitivelyThe strcasecmp() function operates exactly like strcmp(), except that its comparison is case insensitive.Its prototype follows:int strcasecmp(string str1, string str2)The following example compares two e-mail addresses, an ideal use for strcasecmp() because casedoes not determine an e-mail address’s uniqueness:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!