0% found this document useful (0 votes)
4 views

Working with strings

The document provides an overview of string manipulation in PHP, covering functions for determining string length, comparing strings, and changing string case. Key functions discussed include strlen(), strcmp(), strcasecmp(), strtolower(), strtoupper(), ucfirst(), and ucwords(), along with examples of their usage. These functions are essential for handling strings effectively in PHP applications.

Uploaded by

Midhun Manoj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Working with strings

The document provides an overview of string manipulation in PHP, covering functions for determining string length, comparing strings, and changing string case. Key functions discussed include strlen(), strcmp(), strcasecmp(), strtolower(), strtoupper(), ucfirst(), and ucwords(), along with examples of their usage. These functions are essential for handling strings effectively in PHP applications.

Uploaded by

Midhun Manoj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

WORKING WITH STRINGS

1.Determining the Length of a String


Determining string length is a repeated action within countless applications. The
.PHP function strlen() accomplishes this task quite nicely. This function returns
the length of a string, where each character in the string is equivalent to one unit.

Its prototype follows:


strlen(string str)
eg:
<?php
echo strlen("Helloworld!");
?>
Outputs
11
2.Comparing Two Strings
String comparison is arguably one of the most important features of the string-
handling capabilities of any language. Although there are many ways in which two
strings can be compared for equality, PHP provides four functions for performing
this task: strcmp(), strcasecmp(), strspn(), and strcspn().

3. Comparing Two Strings Case Sensitively


The strcmp() function performs a binary-safe, case-sensitive comparison of two
strings. Its prototype follows:
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 str1

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.

Its prototype follows:


string strtolower(string str)
eg:-

<?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

I ANNOY PEOPLE BY CAPITALIZING E-MAIL TEXT.

7. Capitalizing the First Letter of a String


The ucfirst() function capitalizes the first letter of the string str, if it is alphabetical.
Its prototype
follows:

string ucfirst(string str)


eg:-

<?php
$sentence = "the newest version of PHP was released today!";
echo ucfirst($sentence);
?>

Output
The newest version of PHP was released today!

8. Capitalizing Each Word in a String


The ucwords() function capitalizes the first letter of each word in a string. Its
prototype follows:
string ucwords(string str)
eg:-
<?php
$title = "O'Malley wins the heavyweight championship!";
echo ucwords($title);
?>
Output

O'Malley Wins The Heavyweight Championship!

You might also like