A string is a sequence of characters, like "Hello world!".
PHP String Functions
In this chapter we will look at some commonly used functions to manipulate strings.
strlen[] - Return the Length of a String
The PHP strlen[]
function returns the length of a string.
Example
Return the length of the string "Hello world!":
Try it Yourself »
str_word_count[] - Count Words in a String
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!":
Try it Yourself »
strrev[] - Reverse a String
The PHP strrev[]
function reverses a string.
Example
Reverse the string "Hello world!":
Try it Yourself »
strpos[] - Search For a Text Within a String
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.
Example
Search for the text "world" in the string "Hello world!":
Try it Yourself »
Tip: The first character position in a string is 0 [not 1].
str_replace[] - Replace Text Within a String
The PHP str_replace[]
function replaces some characters with some other characters in a string.
Example
Replace the text "world" with "Dolly":
Try it Yourself »
Complete PHP String Reference
For a complete reference of all string functions, go to our complete PHP String Reference.
The PHP string reference contains description and example of use, for each function!
PHP Exercises
PHP String
PHP string is a sequence of characters i.e., used to store and manipulate text. PHP supports only 256-character set and so that it does not offer native Unicode support. There are 4 ways to specify a string literal in PHP.
- single quoted
- double quoted
- heredoc syntax
- newdoc syntax [since PHP 5.3]
Single Quoted
We can create a string in PHP by enclosing the text in a single-quote. It is the easiest way to specify string in PHP.
For specifying a literal single quote, escape it with a backslash [\] and to specify a literal backslash [\] use double backslash [\\]. All the other instances with backslash such as \r or \n, will be output same as they specified instead of having any special meaning.
For Example
Following some examples are given to understand the single quoted PHP String in a better way:
Example 1
Output:
Hello text within single quote
We can store multiple line text, special characters, and escape sequences in a single-quoted PHP string.
Example 2
Output:
Hello text multiple line text within single quoted string Using double "quote" directly inside single quoted string Using escape sequences \n in single quoted string
Example 3
Output:
trying variable $num1 trying backslash n and backslash t inside single quoted string \n \t Using single quote 'my quote' and \backslash
Note: In single quoted PHP strings, most escape sequences and variables will not be interpreted. But, we can use single quote through \' and backslash through \\ inside single quoted PHP strings.
Double Quoted
In PHP, we can specify string through enclosing text within double quote also. But escape sequences and variables will be interpreted using double quote PHP strings.
Example 1
Output:
Hello text within double quote
Now, you can't use double quote directly inside double quoted string.
Example 2
Output:
Parse error: syntax error, unexpected 'quote' [T_STRING] in C:\wamp\www\string1.php on line 2
We can store multiple line text, special characters and escape sequences in a double quoted PHP string.
Example 3
Output:
Hello text multiple line text within double quoted string Using double "quote" with backslash inside double quoted string Using escape sequences in double quoted string
In double quoted strings, variable will be interpreted.
Example 4
Output:
Heredoc
Heredoc syntax [demo example. Now, I am printing {$heredocExample->example[1]}. It will print a capital 'A': \x41
Note: newdoc supported by PHP 5.3.0+ versions.
Invalid Example
We cannot use any whitespace or tab before and after the identifier and semicolon, means identifier must not be indented. The identifier must begin from the new line. It is also invalid in newdoc same as heredoc.
This code will generate an error.
Output:
Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\xampp\PMA\newdoc.php on line 7