In PHP, strings are enclosed in either double quotation marks or single quotation marks.
echo |
Note: There is a significant difference between double quotes and single quotes in PHP. Double quotes process special characters, while single quotes do not. |
You can use either double or single quotes, but you should be aware of the differences between them.
Double-quoted strings interpret special characters.
For example, when a variable is included in a double-quoted string, it returns the value of the variable:
Double-quoted string literals process special characters:
$x |
Single-quoted strings do not perform such actions; they return the string exactly as written, including the variable name.
Single-quoted string literals return the string exactly as it is:
$x |
The PHP strlen() function returns the length of a string.
Return the length of the string “Hello world!”:
echo strlen(“Hello world!”); |
The PHP str_word_count() function counts the number of words in a string.
Count the number of words in the string “Hello world!”:
echo
|
The PHP strpos() function searches for specific text within a string.
If a match is found, it returns the character position of the first match. If no match is found, it returns FALSE.
Search for the text “world” within the string “Hello world!”:
echo strpos(“Hello world!”, “world”); |
Tip: The position of the first character in a string is 0 (not 1).