Strings can be seen as a stream of characters. For example, ‘T’ is a character and ‘Tutorialpath’ is a string. We have learned about basics of string data type in PHP in PHP | Data types and Variables. In this article we will discuss about strings in details. Every thing inside quotes , single (‘ ‘) and double (” “) in PHP is treated as a string.
There are two ways of creating strings in PHP:
- Single-quote strings: This type of strings does not processes special characters inside quotes.
2. Double-quote strings : Unlike single-quote strings, double-quote strings in PHP is capable of processing special characters.
- In the above program we can see that the double-quote strings is processing the special characters according the their properties.
- The ‘\n’ character is not printed and is considered as a new-line. Also instead of the variable name $site, “Tutorialpath” is printed.
–> PHP treats everything inside double quotes(” “) as Strings.
–> In this article, we will learn about the working of the various PHP string functions and how to implement them along with some special properties of strings. Unlike other data types like integers, doubles etc.
–> Strings do not have any fix limits or ranges. It can extend to any length as long as it is within the quotes.
Some of the important and frequently used special characters that are used with double-quoted strings are explained below:
- The character beginning with a backslash(“\”) are treated as escape sequences and are replaced with special characters. Here are few important escape sequences.
- “\n” is replaced by a new line
- “\t” is replaced by a tab space
- “\$” is replaced by a dollar sign
- “\r” is replaced by a carriage return
- “\\” is replaced by a backslash
- “\”” is replaced by a double quote
- “\’” is replaced by a single quote
- The string starting with a dollar sign(“$”) are treated as variables and are replaced with the content of the variables.
Built-in String functions
Built-in functions in PHP are some existing library functions which can be used directly in our programs making an appropriate call to them. Below are some important built-in string functions that we use in our daily and regular programs:
The PHP String function are part of the PHP core.
- strlen() function: This function is used to find the length of a string. This function accepts the string as argument and return the length or number of characters in the string.
2. strrev() function: This function is used to reverse a string. This function accepts a string as argument and returns its reversed string.
3. strpos() function: This function takes two string arguments and if the second string is present in the first one, it will return the starting position of the string otherwise returns FALSE.