PHP Strings
PHP Strings
A string is a sequence of characters, like "Hello world!".
In PHP, strings are surrounded by either double quotes, or single quotes.
Note: There is a difference between double quotes and single quotes in PHP.
Double or Single Quotes?
You can use double or single quotes, but you should be aware of the differences between the two.
A double quoted string will substitute the value of variables, and accepts many special characters, like \n, \r, \t by escaping them.
Example
A double quoted string will substitute the value of variables:
$x = "John";
echo "Hello $x"; // Returns Hello John
Try it Yourself »
A single quoted string does not substitute the value of variables, and will output the string as it was written:
Example
A single quoted string outputs the string as it is:
$x = "John";
echo 'Hello $x'; // Returns Hello $x
Try it Yourself »
Differences between Single and Double Quotes
| Feature | Single Quotes | Double Quotes |
|---|---|---|
| Variable interpolation | No - variables like $x are output literally | Yes - variables are replaced with their values |
| Escape sequences | Only \' and \\ are supported | Supports many, like: \n, \t, \r, \$, \" |
| Performance | Slightly faster (PHP does not need to parse the content) | Slightly slower (PHP must scan for variables and escape sequences) |
| Readability | Cleaner for simple, constant strings | More readable for strings with many variables (do not need to use the concatenation operator (.)) |
Example
See some differences beween double and single quotes:
// Using double quotes
$x = "John";
echo "Hello $x\n";
echo "\tHow are you?\n";
// Using single quotes
$x = 'John';
echo 'Hello $x\n';
echo '\tHow are you?\n';
Try it Yourself »
String Length
The PHP strlen() function returns the length of a string.
Example
Return the length of the string "Hello world!":
echo strlen("Hello world!");
Try it Yourself »
Word Count
The PHP str_word_count() function counts the
number of words in a string.
Example
Count the number of word in the string "Hello world!":
echo str_word_count("Hello world!");
Try it Yourself »
Search For Text Within a String (PHP 8)
The PHP
str_contains() function checks if a string contains a specific
substring.
If a match is found, the function returns a boolean true. If no match is found, it will return a boolean false.
Example
Search for the text "love" in the string "I really love PHP!":
$txt = "I really love PHP!";
var_dump(str_contains($txt, "love"));
Try it Yourself »
Note: This function performs a case-sensitive search.
The following example will return a boolean false, because "Love" is not found in the main string:
Example
Search for the text "Love" in the string "I really love PHP!":
$txt = "I really love PHP!";
var_dump(str_contains($txt, "Love"));
Try it Yourself »
Search For Text Within a String (PHP 7)
The PHP strpos() function searches for a specific text within a string.
If a match is found, the function returns the character position of the first match. If no match is found, it will return false.
Note: This function performs a case-sensitive search.
Example
Search for the text "world" in the string "Hello world!":
echo strpos("Hello world!", "world");
Try it Yourself »
Tip: The first character position in a string is 0 (not 1).
Complete PHP String Reference
For a complete reference of all string functions, go to our complete PHP String Reference.