Introduction to PHP – Part6

advertisement

Strings:

A string is an array of characters. This tutorial demonstrates the use of string to manipulate text. A string can be represented in one of the following four ways; single quoted, double quoted, heredoc syntax and nowdoc syntax. Before describing each string in detail, it would be appropriate to discuss about escape sequence. Escape sequence are used to format the output text and other output options. The table below presents the escape sequences for special characters.

Sequence Explanation
\n Linefeed
\r Carriage return
\t Horizontal tab
\v Vertical tab
\f Form feed
\\ Backslash
\$ Dollar sign
\” Double quote

There are many functions available in PHP to manipulate strings. For a complete list of reference on functions please visit the link http://www.w3schools.com/PHP/php_ref_string.asp

Single quoted

A string can be represented in single quote. In PHP a single quote string can be expanded in more than one line. Remember that, if we attempt to escape the character other than the above table, it will print the backslash too.

Example#1

<html>
<body>
<?php
//Output: Hello World
$myString = 'Hello World';
echo $myString;
//or we can simply write
echo 'Hello World';

//To print string enclosed in double quotation, simply enclose it with single quotation
//Output: "Hello World"
$myString = '"Hello World"';
echo $myString;
 
//To print string that contains single quotation, simply use escape sequence "\" in front of single quote
//Output: "Hello World, let's begin with more complex examples"
$myString = '"Hello World, let\'s begin with more complex examples"';
echo $myString;
?>
</body>

Double quoted
PHP recognized all of the above escape sequences if it is enclosed in double quotes. Remember that, as in single quoted string, if we attempt to escape the character other than the above table, it will print the backslash too.

Example #2

<html>
<body>
<?php
//Output: Hellow World
//This is similar to the example presented in Example#1, except that it is enclosed by double quote
$myString = "Hello World";
echo $myString;
//or we can simply write
echo "Hello World";
 
//Similar to this example, \v \t \r can be implemented
//Output: Hellow World
//This will print in a new line
$myString = "Hello World \n This will be printed in a new line";
echo $myString;

//This example demonstrate how variables can be expanded using double quote string
//Output: The value of $x is 10 and value of $y is 20. The result of  $x + $y is 30
$x=10;
$y=20;
$z=$x+$y;
//In the following statement, concatenation operator "." is used to concatenate two strings. The variable $x in single quote is not expanded where it is expanded in double quote
echo 'The value of $x is '."$x". ' and value of $y is '. "$20" .'. The result of  $x + $y is '."30";
?>
</body>

heredoc

heredoc syntax <<< can also be used to represent string. heredoc text behaves like double quote string, except that it is not enclosed with double quote, that means that escape sequence can be used in heredoc in the similar manner. The variables are also expanded in heredoc text.

Example#3

<html>
<body>
<?php
//Output: Hellow World, This is your heredoc example, and this can be expanded as necessary
//As shown in the code, after <<< an identifier (here ID) should be provided and then a newline should begin.
//Then the string comes and at last the same identifier is used to close the quotation.
$myString = <<<ID
Hellow World
This is your heredoc example,
and this can be expanded as necessary
ID;
echo $myString;
?>
</body>

nowdoc

heredoc text behaves like double quote string while nowdoc text behaves like single quote string. The main difference is that  escape sequence cannot be used in nowdoc.

Example#4

<html>
<body>
<?php
//Output: Hellow World, This is your nowdoc example, and this can be expanded as necessary
//To differenciate between heredoc and nowdoc, identifier is enclosed in single quote in nowdoc as shown
$myString  = <<<'ID'
Hellow World
This is your nowdoc example,
and this can be expanded as necessary
ID; */
echo $myString;
?>
</body>

 

 

 

Strike down your 000-152 exam pressure through joining our most recent pass4sure 70-630 online courses and free 646-671 exam dumps.

About the Author:

Bhoj R Dhakal is a programmer. He has few years of experience in software development and maintenance. He has published series of PHP tutorials for webanddesigners.