PHP strings
A String is a grouping of characters sorrounded by double quotes such as "this is a string".
Strings are an important part of PHP. But there is much more that can be done with them than just printing them onto a webpage. PHP provides many powerful String functions which can be used to manipulate your text in many ways.
This tutorial focuses on:
- Creating strings
- Printing strings
- Joining strings
- String functions
Creating strings
You can create a string by declaring a string variable. The value of a string variable should be text sorrounded by double quotes.
Printing strings
You can print a string by referencing the variable which stores the strings value with a print or echo statement. The name of the variable can be sorrounded by double quotes or not.
Joining strings
You can join two or more strings into one using the dot ( . ) operator. Using this operator you can either print two or more strings together or you can combine two or more strings into one. Joining two or more strings is known as concatenation.
String functions
PHP contains several built-in string functions that you can use to manipulate your text in many ways.
- The strlen() function - Returns the length of a string.
- The substr() function - Returns a section of a string. Takes three parameters - the string to extract from, the position in the string from which to begin extracting, and how many characters to extract.
- The strrev() function - Takes a string and reverses it.
- The strstr() function - Used to find one string in another string. Takes two parameters - the string you are searching in, and the string you are searching for. The strstr() function will return all the content from the start of the found string to the end of the string to search in.