www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons Working with strings in PHP Bookmark and Share
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Lesson 24
Lesson 25
Lesson 26
Lesson 27
Lesson 28
Lesson 29
Lesson 30
Lesson 31
Lesson 32
Lesson 33
Lesson 34
Lesson 35
Lesson 36
Lesson 37
Lesson 38
Lesson 39
Lesson 40
Lesson 41
Lesson 42
Lesson 43
Lesson 44
A string variable is used to store and manipulate a piece of text. PHP strings are created using single quote or double quotes.

$myString = 'Single Quote String';
$myString2 = "Double Quote String";

In case the string is a large one and spans multiple lines you could use the PHP heredoc string as under:

$myHeredocString = <<< ABC This is the text that will be included in the myHeredocString variable. Heredoc strings are very useful when dealing with large blocks of string variables.

Note that the ABC after <<< is an identifier and it marks the beginning and end of the heredoc text block. You could use an identifier of your choice in place of ABC. To construct a heredoc string variable, we should provide an identifier after <<<, then the string, and then the same identifier to close the string. The closing identifier must begin in the first column of the line.

String Manipulation
Here we are going to look at some of the most common functions and operators used to manipulate strings in PHP.

Printing Strings
We use the echo statement to output or print a string. The argument to the echo statment can either be a string or a string variable. See the example below:
$txt="Hello World";
echo $txt;
echo "Hello World Again";

The output of the code above will be:
Hello World
Hello World Again

String Concatenation
To join or concatenate two strings we use the concatenation operator (.). The concatenation operator can be used either to join two strings or two string variables. See the example below:

$txt1="Hello World!";
$txt2=" We are back!";
echo $txt1 . " " . $txt2;
The output of the code above will be:
Hello World! We are back!

The strlen() function
We use the strlen() function to find the length of a string. For example we can find the length of the string "Hello world!" as under:

echo strlen("Hello world!");

The output of the code above will be:

Using the strpos() function
The strpos() function is used to search for a string or character within a string. If a match is found in the string, this function will return the position of the first match. If no match is found, it will return FALSE. Let's try to find the string "world" in our string "hello world!":

echo strpos("Hello world!","world");

The output of the code above will be:

If you are a keen observer, you might have noticed that the actual position of the string world in the string hello world! is 7 and not six. So what is heppening here? The answer is simple; PHP starts counting from 0 and not 1!

Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.