A string is a sequence of characters which are enclosed by quotes. Strings consist of characters which may be letters, numbers or special characters. A string is either enclosed by single quotation marks (‘) or double quotation marks(“) because the parser needs a method to distinguish the text of the string from the rest of the program. Depending on the type of quotation mark used a string is processed differently.
Double quotations vs single quotations for strings
A string within single quotes is treated as a literal. Hence no more processing is done on the string and its value is stored as is.
However, a string within double quotes always gets processed by the pre-processor first. Hence strings within double quotes will be evaluated and will get replaced by the value after pre-processing and special character sequences will get interpreted differently. Therefore a string within double quotes will be evaluated as follows:
- Variable names which are a part of the string will get replaced by their value. Hence the value of the variable will be inserted as a string in the main string
- Character sequences which begin with a backslash (escape characters) will be identified and then replaced by special characters corresponding to the character sequence.
Hence escape-sequences will be replaced by special characters as follows:
- \n is replaced by the newline character
- \r is replaced by the carriage-return character
- \t is replaced by the tab character
- \$ is replaced by the dollar sign itself ($)
- \" is replaced by a single double-quote (")
- \\ is replaced by a single backslash (\)
String Concatenation Operator
The dot (.) operator is called as the string concatenation character. It is used between 2 strings when we need to concatenate or join the two strings together.
Example:
In the above example, 3 strings are concatenated by using the concatenation operator twice.
PHP String functions
PHP provides us with a comprehensive set of inbuilt functions which perform a range of operations on strings. These functions are very useful to programmers because strings often need to be extensively manipulated in PHP scripts for a variety of reasons. Some of the common string manipulation functions are as follows :
Strlen()
The strlen() function is used to find the length of a string. It accepts a string, for which the length needs to be found, as the argument and then returns the length of the string. If the string is empty then it returns a 0.
Syntax:
strlen($string_name);
Example:
Str_word_count()
The str_word_count() function provides us with the number of words in a string. This is different from the strlen() function because the strlen() function counts characters including spaces whereas the str_word_count() counts the number of words in the string.
Example:
Strrev()
The strrev() function accepts a string as an argument and then returns the reverse of the string. In short it reverses a string .
Example:
<?php
echo strrev("Learn PHP!");
//The output will be !PHP nraeL because the string 'Learn PHP!' gets reversed.
?>
Strpos() function
The strpos() function is used to find out if a substring exists within a string and then to get the exact position where the substring starts .
The function accepts 2 arguments, the string and the substring which we are trying to locate. If the substring is found then the strpos() function will return the exact position of the first occurrence of the substring within the string. If the substring is not found in the string then the strpos() function returns a FALSE.
Syntax:
The syntax of the strpos function is as follows :
Strpos($string, $substring);
where $ string is the string in which we are trying to locate a substring and $substring is the substring
Example:
Str_replace() function
The str_replace function will replace a substring within a string with a user-defined string.
Syntax:
The syntax of the str_replace() function is as follows :
str_replace ($substring, $string_replace , $string);
where $substring is the substring within the main string which needs to be replaced. $string_replace is the string which needs to be inserted in place of $substring. $string is the main string on which operations need to be performed.
Example:
PHP consists several other prebuilt string processing functions. More detailed documentation about the string functions, usage and functions can be found here.