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 lesson 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.
Syntax:
$varName = "string value";
Example:
<?php $sentence = "The giraffe is an interesting animal."; ?>
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 in double quotes or not.
Syntax:
print $varName; print "$varName"; echo $varName; echo "$varName";
Example:
<?php //declare some strings $sentence1 = "The giraffe is an interesting animal."; $sentence2 = "PHP is a scripting language."; //print the value of the first string print $sentence1; //skip a line echo "<br />"; //print the value of the second string print "$sentence2"; ?>
Output:
PHP is a scripting language.
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.
Syntax:
$aString = $text1 . $text2 . "text" . " more text" print "$text1" . "$text2" . "text" . "text"
Example:
<?php //declare some strings $fragment1 = "The giraffe is "; $fragment2 = "an interesting animal."; //declare a third string and join the first //two strings into it $sentence1 = $fragment1 . $fragment2; //print the value of $sentence1 //which is a combination of $fragment1 and $fragment2 print $sentence1; //skip a line echo "<br />"; //print four strings together print "PHP is " . "a scripting " . "language" . "."; ?>
Output:
The giraffe is an interesting animal.
PHP is a scripting language.
String functions
PHP contains several built-in string functions that you can use to manipulate your text in many ways.
-
The strlen() function
The strlen() function takes a string as its parameter and returns the strings length.
Syntax:
strlen($aString);
Example:
<?php $stringOne = "PHP is cool!";
$length = strlen($stringOne); print $length; ?>Output:
12In the above example, a string named $stringOne is declared with the value "PHP is great!", and a variable named $length is declared which takes the value returned by the strlen() function. The parameter passed to the strlen() function is $stringOne. The strlen() function will return the length of $stringOne and place that value in the $length variable. The length of the $stringOne variable is then printed. PHP counts all characters in a string, including spaces. For this reason, the length of $stringOne will be 12.
-
The substr() function
The substr() function returns a section of a string and takes three parameters. The first parameter is the string to extract from. The second parameter is the position in the string from which to begin extracting. The third parameter is how many characters to extract.
Syntax:
substr($theString, beginningPosition, numCharactersToExtract);Example:
<?php $stringOne = "PHP has many great string functions"; $stringExtracted = substr($stringOne, 0, 3); print $stringExtracted; ?>Output:
PHPIn the above example, the substr() function returns part of the $stringOne string variable beginning at the zeroeth position and ending at the second position. The $stringExtracted variable receives this value and it will be "PHP". The $stringExtracted variable is then printed.
-
The strrev() function
The strrev()function takes a string and reverses it.
Syntax:
strrev($aString);
Example:
<?php $stringOne = "Reverse this string"; $stringReversed = strrev($stringOne); print $stringReversed; ?>Output:
gnirts siht esreverIn the above example, the strrev() function takes the string specified in the $stringOne variable, reverses it, and places the result in the $stringReversed variable. The returned value would be "gnirts siht esreveR". The $stringReversed variable is then printed.
-
The strstr() function
The strstr() function is used to find one string in another string. It takes two parameters. The first parameter is the string you are searching for, and the second parameter is the string you are searching in.
Syntax:
strstr($theStringToFind, $theStringToSearchIn);
Example:
<?php print strstr("apple", "apple, orange, banana, mango"); ?>Output:
appleIn the above example, the strstr() function searches for the string "apple" inside the string "apple, orange, banana, mango"




