String Data Type in PHP
PHP Data Types Simplified – Part 2
Forward: In this part of the series we look at the string data type in PHP.
By: Chrysanthus Date Published: 10 Aug 2012
Introduction
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
String Basics
A string is a series of characters. A string literal can be specified in 4 different ways, but we shall look only at two: single quoted, double quoted heredoc. The argument in the echo construct below is a string literal in single quotes:
echo 'His name is John.';
The argument below is a string literal in double quotes:
echo "His name is $hisVar";
With double quotes, the value of a variable in the string literal will appear, in place of the variable, when the string is displayed. So if we had,
$hisVar = "Peter";
then the above string will be displayed as, "His name is Peter". With single quotes, the value of a variable in the string literal does not replace the variable. With single quotes, the variable name including the $ sign is displayed and not the value.
Let us now look at some string functions.
The strlen function returns the length of a string as number of characters, including spaces. The syntax is:
int strlen (string $string)
The following statement displays 14.
echo strlen("I am a string.");
Of course, you can assign the return value of the function to a variable, and then use the variable for some other manipulation.
The strpos Function
Character position counting in a string begins from zero. Character position of a character in a string is also called the index position. The strpos function returns the position of the first occurrence of a sub string. In simple terms, the syntax is:
int strpos(mainString, subString)
The following statement displays, 3:
echo strpos("We are dancing.", "are");
The substr Function
Remember, character position counting in a string begins from zero. The substr function returns a sub string whose start position is given. The syntax is:
string substr ( string $string , int $start [, int $length ] )
If the optional length parameter is omitted, the sub string from the start position to the end is returned.
The following statement displays, “are”.
echo substr("They are dancing.", 5, 3);
The main string is "They are dancing." The start position is 5. The length (number of characters) is 3. So “are” is displayed.
There are many string functions. I have given you just three. The string functions are predefined in PHP. You need a whole series in order to understand all the string functions. Search my blog for such a series.
As of PHP 5.0, the string literal can be specified in three ways: the single quoted, the double quoted and the heredoc syntax ways. We have just seen the single and the double quoted ways. Let us look at the heredoc syntax way.
Here we see how to delimit strings using the heredoc syntax. heredoc syntax is used when you want to maintain the format the text (string) is typed with the program in the text editor (or some other editor). The lines are maintained as typed. The spaces are also maintained as typed. In addition you can type single and double quotes without escaping the quotes. Variables typed are also expanded (replaced with their values).
I begin with an example. In order to understand and try this example, you need basic knowledge in PHP File Handling. If you do not have that knowledge, then read the article in this blog titled, “PHP File Handling Basics”. Consider the following program:
<?php
$var = "man";
$aStr = <<<EOSTR
The $var is standing on the hill.
The woman at his side is his wife.
They have two children who are not with them now.
"I am complete." the man once said.
EOSTR;
$myHandle = fopen("myfile.txt", "x");
fwrite($myHandle, $aStr);
fclose($myHandle);
echo "File has been saved.";
?>
With the syntax, you have a string, which can be assigned to a variable as in the above program where the variable assigned to is, $aStr. The first delimiter is <<< followed immediately by an identifier of your choice. You do not begin the identifier with a $ sign. After typing the identifier, you press the enter key. Then you type your text (string) in the format (lines and spaces including quotes and variables) you want. After that you press the enter key, type the identifier again immediately followed by the semicolon; and press the Enter key once more. In other words, the ending delimiter is preceded by a “newline”, no space, and is on a separate line with its semicolon. Note: whenever you press the enter key in a text editor, you are typing a hidden character known as the new line character.
Read and try the above program, and a file with the name, myfile.txt will be saved in your working directory. The file should not already exist. Open the file with your text editor and note that the formatting has been maintained and the variable has been expanded.
The characters of a string can be likened to the elements of an array. You can access and modify the characters of a string, as you would do for an array. The following program illustrates this (read and try it):
<?php
$str = "I am a man.";
echo $str[7]."<br />";
echo $str[8]."<br />";
echo $str[9]."<br />";
$str[7] = 'b';
$str[7] = 'o';
$str[7] = 'y';
echo $str;
?>
Let us take a break now and continue in the next part of the series.
Chrys
Related Links
Major in Website DesignWeb Development Course
HTML Course
CSS Course
ECMAScript Course
PHP Course
NEXT