Strings are so commonly used in PHP that there are a number of library functions provided to manipulate them.
String Literals
String literals may be enclosed with single or double quotes but behave differently if variables are included within the string.
With double quotes the variable can be written directly into the string - with single quotes concatenation must be used.
Examples of Single and Double Quotes
<?php
$name = "George";
$age = 35;
// Using double quotes
echo ("His name is $name aged $age years");
// Using single quotes
echo ('His name is '.$name.' aged '.$age." years");
?>
Escape Sequences
Escape sequences are preceded by the \ character to signify that the characters are to be treated differently.
In a single quoted string if you wish to use the ' character then to avoid PHP treating the character as a string delimiter then it must be escaped with the \ character.
The only other escape sequence supported by single quoted strings is the \ character itself, which if used, must be escaped with \.
Single Quotes Escape Sequences
<?php
echo ('It\'s a single quote string.');
echo ('Backslash \\ is used as an escape character.');
?>
Double quoted strings support a number of escape sequences including ...
\" to escape the string delimiter.
\\ to escape the escape character.
\$ to escape variable names.
\t to output a tab character.
\n to output a newline character.
\r to output a carriage return character.
Finding the Length of a String
The strlen() function returns the number of characters in a string.
Example of strlen()
<?php
$name = "George";
// This prints out 6
echo (strlen ($name));
?>
Changing Case
You can change the case of all or part of a string with the following functions.
strtolower() changes all characters in the string to lowercase.
strtoupper() changes all characters in the string to uppercase.
ucfirst() changes first character in string to uppercase.
ucword() changes first character of each word in string to uppercase.
Comparing Strings
PHP provides a number of functions for comparing two strings to determine if they are equal or one is greater than the other.
A string which is greater than another is one whose characters appear later in the alphabet. e.g. Fred is greater than Frank.
The strcmp() Function
This takes two strings as arguments and returns ...
0 if the strings are identical.
-1 if the first string is less than the second string.
1 if the first string is greater than the second string.
Example of strcmp()
<?php
$name1 = "George";
$name2 = "George";
// This prints out 0
echo (strcmp ($name1, $name2));
?>
The strncmp() Function
This works in the same way as strcmp() except it accepts an extra length argument to tell the function to restrict the comparison to the number of characters stated.
Example of strncmp()
<?php
$name1 = "George";
$name2 = "Georgina";
// This prints out 0
echo (strcmp ($name1, $name2, 6));
?>
The functions strcasecmp() and strncasecmp() do the same as strcmp() and strncmp() but are not case sensitive.