Working with strings
Working with strings
Eg:
<?php
$pswd = "supersecret";
$pswd2 = "supersecret2";
if (strcmp($pswd, $pswd2) != 0) {
echo "Passwords do not match!";
} else {
echo "Passwords match!";
}
?>
4. Comparing Two Strings Case Insensitively
The strcasecmp() function operates exactly like strcmp(), except that its
comparison is case insensitive.
Its prototype follows:
int strcasecmp(string str1, string str2)
eg:
The following example compares two e-mail addresses, an ideal use for
strcasecmp() because case does not determine an e-mail address’s uniqueness:
<?php
$email1 = "[email protected]";
$email2 = "[email protected]";
if (! strcasecmp($email1, $email2))
echo "The email addresses are identical!";
?>
In this example, the message is output because strcasecmp() performs a case-
insensitive comparison of $email1 and $email2 and determines that they are
indeed identical.
5. Manipulating String Case
Four functions are available to aid you in manipulating the case of characters in a
string: strtolower(), strtoupper(), ucfirst(), and ucwords().
Converting a String to All Lowercase
The strtolower() function converts a string to all lowercase letters, returning the
modified string. Nonalphabetical characters are not affected.
<?php
$url = "http://WWW.EXAMPLE.COM/";
echo strtolower($url);
?>
6. Converting a String to All Uppercase
This is accomplished with the function strtoupper(). Its prototype follows:
string strtoupper(string str)
eg:-
<?php
$msg = "I annoy people by capitalizing e-mail text.";
echo strtoupper($msg);
?>
Output
<?php
$sentence = "the newest version of PHP was released today!";
echo ucfirst($sentence);
?>
Output
The newest version of PHP was released today!