PHP string is a sequence of letters, numbers, special characters i.e. used to store and manipulate text. In this tutorial, we will discuss strings in detail. Everything inside quotes, single (‘ ‘) and double (” “) in PHP is considered as a string.
To create a string in PHP, add a text inside a single-quote. It is a simple way to specify string in PHP. This type of strings does not process special characters inside quotes as shown in the below example:
In PHP, we can create string through enclosing text within double quote also. This type of strings is capable of processing special characters.
Following are the some of important escape sequences that are used with double-quoted strings:
Calculating the Length of a String:
The strlen() function is used to calculate the length of string. It also count the blank space inside the string.
Reversing a String:
The strrev() function returns a reverse string. This function takes a string as an argument and returns its reversed string.
The str_replace() function replaces all occurrences of the search text within the target string.
Counting Number of Words in a String:
The str_word_count() function calculate the number of words in a string.
PHP String Functions:
|1||strlen()||Calculates the number of characters in a string|
|2||str_repeat()||Repeats a string no. of times you want|
|3||strrev()||Retrun reverse of a given string|
|4||substr()||Retrieves a section of a string|
|5||str_word_count()||Calculates the number of words in a string|
|6||strcmp()||Compares two strings|
|7||trim()||removes leading and trailing whitespaces from a string|
|8||str_replace()||Replaces parts of a string|
|9||strtoupper()||Converts in Uppercases a string|
|10||strtolower()||Converts in Lowercases a string|