PHP TUTORIALPHP TutorialPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP CommentsPHP VariablesPHP Variable ScopePHP ConstantsPHP StringsPHP OutputPHP Data TypesPHP Type CastingPHP OperatorsPHP ConditionalsPHP Shorthand ConditionalsPHP LoopsPHP Loop Control StructuresPHP FunctionsPHP String FunctionsPHP ArraysPHP Superglobal VariablesPHP in HTMLPHP RegexRegex IntroductionRegex PCRE SyntaxPHP PREG FunctionsPHP FormsPHP Forms IntroductionPHP Forms CreatingPHP Forms SecurityPHP Forms ValidationPHP Forms Required InputsPHP Forms StickyPHP Forms Advanced ValidationPHP Forms FinishingPHP OOPPHP Object Oriented ProgrammingPHP OOP ClassesPHP OOP PropertiesPHP OOP ObjectsPHP OOP MethodsPHP OOP $this KeywordPHP OOP Constructors and DestructorsPHP OOP VisibilityPHP OOP InheritancePHP OOP Abstract Classes and MethodsPHP OOP Interfaces

PHP Strings

A string is any finite sequence of characters (letters, numerals, symbols, punctuation marks, etc.)

Declaring Strings

There are three ways.

Single Quoted

The easiest way to declare a string is by enclosing it by single quotes. Character: '


$string = 'This is my first string';

What if you needed to add a single quote inside a single quoted string? Just escape that character with a back slash. \'


# This is the Hyvor's PHP Tutorial
$string = 'This is the Hyvor\'s PHP Tutorial';

What if you needed to have \' in a single quoted string? Use \\ to escape the \ character. And, as in the last example, \' to escape the ' character.


# Backslash and Quote: \'
$string = 'Backslash and Quote: \\\'';

Escaping is used to reduce ambiguity and give special meaning when using quotes. These characters are called escape sequences.

Double Quoted

Strings can be declared enclosed by double quotes. Character: ".

In single quotes, only \\ and \' had a special meaning. But, in double quotes, there are more escape sequences.

Escape SequenceMeaning
\nLine Break
\rCarriage Return
\tTab Space
\\Backslash
\$Dollar Sign
\"Double Quotes
All escape characters starts with a backslash \.

PHP Double Quoted String Example


<pre>
<?php
echo "Hello World";
echo "<br>";
echo "\"Hello World\"";
echo "\n\tHello World\n";
?>
</pre>

Run Example ››

Tip: The reason we have echoed out the output from PHP inside <pre> tags is those tags allows you to show the text as it is in the browser. (By default, HTML ignores extra white spaces)

In the table, you may notice the $ (dollar) sign. Why is it there? The reason is that we can add variables inside double quoted texts

Variables within Double Quoted Strings

When you add a variable within a double quoted string, PHP will parse it.

PHP Variables in Double Quoted String


<?php
$name = "Hyvor";
echo "My name is $name";

Run Example ››

If we needed to add a s character in front of a variable in the same double quoted string, we will need to wrap the variable with curly braces.

PHP Variables in Double Quoted String - Complex Syntax


<?php
$fruit = 'Apple';
// echo "$fruits"; # there's no variable $fruits
echo "{$fruit}s";

Run Example ››

PHP Single vs. Double Quoted Strings

'"
Will be treated literally, displays exactly what you typeWill be interpreted
Does not work with escape sequences, except \' and \\Works with escape sequences
Variables inside are not parsedVariables inside are parsed

<?php
// both works
echo "Hello <br>";
echo 'Hello <br>';

// escape sequences are not parsed in single quotes
echo "\"World\" <br>"; // outputs "World"
echo '\"World\" <br>'; // outputs \"World\"

// variables are not parsed in single quotes
$hello = "Hello";
echo "$hello World <br>"; // outputs Hello World
echo '$hello World <br>'; // outputs $hello World

Run Example ››

Doc Syntax

Doc Syntax is the next way of declaring a string. It has two types which each works like single quoted and double quoted strings.

Doc Syntax

Identifier is any valid PHP label. The same naming rules you learned in the constants chapter applies to this identifier. (Should start with a non-digit character or underscore and must only contain alpha-numeric characters and underscores)

Nowdoc

Nowdoc string is declared using the doc syntax and single quotes for the identifier.

PHP Nowdoc Example


<?php
$string = <<<'MYSTRING'
I'm a nowdoc string
Did you notice that I can write '' without escaping?
I don't have special meaning for \n, \r and \t
I do not parse variables $name
MYSTRING;
echo '<pre>' . $string . '</pre>';

Run Example ››

Nowdoc strings behaves like single quoted strings. There is no special meaning for escape sequences. Variables are not parsed.

Heredoc

Heredoc string is declared using the doc syntax and double quotes (or without quotes) for the identifier.

PHP Heredoc Example


<?php
$name = 'Hyvor';
$string = <<<STR
I'm a heredoc
I parse variables. (I love $name)
\t \\t adds a tab space. That means I accept escape sequences
STR;

echo '<pre>' . $string . '</pre>';

Run Example ››

Heredoc strings behaves like double quoted strings. It converts escape sequences to their real meaning. And, variables will be parsed.

The best thing about doc syntax is that we don't have to escape quotes inside them. This makes writing HTML, XML, SQL, etc. strings easy.
Facebook Twitter