w3resource
PHP Tutorial

PHP Data Types: Strings

Description

Strings are a sequence of characters. In PHP, a character is the same as a byte, therefore there are exactly 256 different characters possible. The long string is supported in PHP, in fact, there is no practical bound to the size of strings. But PHP has no native support for Unicode.

A string literal can be specified in three different ways.

Single quoted
Double quoted
Heredocs

PHP Strings: Single quoted

The simple way to print a string is to enclose it in a single quote ( use the character ' ). If you want to print a single quote (') within a string then you must escape it with a backslash like many other languages. If a backslash (\) needs to print before a single quote or at the end of the string then the backslash must occur twice. At the end of the every string, a html break character has added for line breaking.

Example:

<?php
echo 'One line simple string.<br />';
echo 'Two line simple
string example<br />';
echo 'Tomorrow I \'ll learn PHP global variables.<br 
/>';
echo 'This is a bad command : del c:\\*.* <br />';
?>

Explanation:

The above echo statements display single quoted strings. In the second echo statement, we have written the string in two lines though it will be displayed in a single line. In the third echo statement a single quotation character (') is printed for the backslash character. In the fourth echo statement, a backslash character(\) has printed after c:, here we have taken two backslash characters together.

View this example in browser

PHP Strings : Double quoted

When we want to print some special characters or the values of variables within a string then we enclose the string with double-quotes(") character.

itemscope itemtype="http://schema.org/WebPageElement/Heading">Following character sequences beginning with a backslash (\) are replaced with special character

Escape-sequence Replaced by
\n linefeed (LF)
\r carriage return (CR)
\t horizontal tab  (HT)
\v vertical tab (VT)
\f form feed (FF)
\\ backslash
\$ dollar sign
\" double-quote

Example :

<?php
echo "Example ::\n Linefeed string <br />";
echo "Example ::\r Carriage return <br />";
echo "Example ::\t Horizontal tab <br />";
echo "Example ::\v Vertical tab <br />";
echo "Example ::\f Form feed <br />";
echo "Example ::\\ Backslash <br />";
echo "Example ::\" Double quote\" <br 
/>";
echo "$ is replaced by the dollar sign itself <br
/>";
echo "Single quote within a double quote : I 'll go 
tomorrow.<br />" ;
echo "This is bad command : del c:\*.*";
?>

View this example in browser

Variable in double quoted string :

Whenever a $ symbol appears within a string, PHP tries to read the immediate part followed the $ character as a variable name. If the variable type is a string, that string is inserted into the string in that position, if the variable type is a non-string then it is converted into string type.

Example:

<?php
$samt=2000;
$desc="My salary amount for this month is: ";
echo "$desc $samt <br />";
// We can write the above example in the following way
echo "My salary amount for this month is: $$samt";
?>

View this example in browser

PHP Strings: heredocs

In addition to the single-quote and double-quote syntaxes, there is an another way to embed large pieces of text in your scripts which may include lots of double and single quotes.

Syntax:

<<<identifier
....text here .......
....text here .......
....text here .......
identifier

Example:

<?php
$mystring=<<<MYID
"Tomorrow I 'll not go at your house"
"may be another day."
'Thank you'
MYID;
echo $mystring;
?>

View this example in browser



New Content: Composer: Dependency manager for PHP, R Programming