PHP Strings are groups of characters, like \”PHP supports string operations.\”
NOTE: Built-in string functions are listed in PHP String Functions Function Reference.
Here are some good examples of string:
$var= \”This is a string with double quotes\”;
$var_2 = \”This is a string that is a bit longer and has only one quote\”;
$var_39 = \”This string has thirty-nine characters\”;
$var_0 = \”\”; / a string with no characters
Single-quoted strings are almost taken at face value, while double-quoted strings replace variables with their values and treat certain character sequences in a special way.
PHP Strings Example
<?php
$string= \”Hello\”;
$val= \’$string will not print!\\\\n\’;print($val);
print \”<br />\”;$literally = \”$string will print!\\\\n\”;
print($val);
?>
Output
$string will not print!\\n
Hello will print!\\n
There are no artificial limits on how long a string can be. As long as you have enough memory, you should be able to make strings as long as you want.
PHP does the following two things to strings that are separated by double quotes, like \”this\”:
Some sequences of characters that start with a backslash () are replaced by special characters.
Variable names that start with $ are changed to strings that show what their values are.
The escape-sequence replacements are −
- \\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 (\”);
- \\n is replaced by the newline character;
Concatenation Operator for Strings
Use the dot (.) operator to join two string variables together.
PHP Strings Example
<?php
$string1=\”Hello User\”;
$string2=\”43210\”;echo $string1 . \” \” . $string2;
?>
Output
Hello User 43210
If you look at the code above, you can see that the concatenation operator was used twice. This is because a third string had to be added.
We put a string with a single character, an empty space, between the two string variables to separate them.
The strlen() function
With the strlen() function, you can find out how long a string is.
Let\’s find out how long our \”Hello String!\” string is. −
PHP strlen Example
<?php
echo strlen(\”Hello String!\”);
?>
Output
13
The length of a string is often used in loops or other functions where it is important to know when the string ends. (That is, we would want the loop to end when the last character in the string is reached.)
The strpos() function
The strpos() function is used to look for a string or character within a string.
This function will return the position of the first match if the string contains a match. It will return FALSE if no match is found.
Let\’s see if our string contains the word \”String.\”
Example
<?php
echo strpos(\”Hello String!\”,\”String\”);
?>
Output
6
As you can see, the string \”String\” is in position 6 of our string. The reason that it is 6, and not 7, is that the first position in the string is 0, and not 1.