String variables are used to store and process text. String variables are used to store and process text.
String variables in PHP
String variables are used to contain character values.
After creating a string, we can operate it. You can use a string directly in a function or store it in a variable.
In the following example, we create a string variable named txt and assign it to "Hello world! ". Then we output the txt variable value:
Instance
Note: When you assign a text value to a variable, remember to add single quotation marks or double quotation marks to the text value.
Now, let's take a look at some common operators and operators that operate strings.
PHP concatenation operator
In PHP, there is only one string operator.
The concatenation operator (.) is used to connect two string values.
The following example shows how to connect two string variables:
Instance
The above code will output: Hello world! What a nice day!
Tip: In the above code, we have used the concatenation operator twice. This is because we need to insert a space between two strings.
PHP strlen () function
Sometimes it is useful to know the length of a string value.
The strlen () function returns the string length (number of characters ).
The following instance returns the string "Hello world! "Length:
Instance
The above code will be output: 12
Note: strlen () is often used in loops and other functions, because it is important to determine when the string ends. (For example, in a loop, we need to end the loop after the last character in the string .)
PHP strpos () function
The strpos () function is used to search for a character or a specified piece of text in a string.
If a match is found in the string, the function returns the first matched character position. If no matching is found, FALSE is returned.
The following instance is in the string "Hello world! "Search text" world ":
Instance
The above code will be output: 6
Tip: In the above instance, the position of the string "world" is 6. The reason is 6 rather than 7 is that the first character in the string is 0 rather than 1.
The above is the content of the PHP string variable. For more information, see The PHP Chinese website (www.php1.cn )!