String In PHP
Summary: A string is a collection of characters. String is one of the data types supported by PHP. The string variables can contain alphanumeric characters. PHP string
A string is a sequence of letters, numbers, special characters and arithmetic values or combination of all. The simplest way to create a string is to enclose the string literal (i.e. string characters) in single quotation marks ('), like this:

$my_string = 'Hello World';
You can also use double quotation marks ("). However, single and double quotation marks work in different ways. Strings enclosed in single-quotes are treated almost literally, whereas the strings delimited by the double quotes replace variables with the string representations of their values as well as specially interpreting certain escape sequences.
A PHP string is a sequence of characters i.e. used to store and manipulate text. There are 4 ways to specify string in PHP.
The escape-sequence replacements are:
- is replaced by the newline character
- is replaced by the carriage-return character
- is replaced by the tab character
- $ is replaced by the dollar sign itself ($)
- " is replaced by a single double-quote (")
- is replaced by a single backslash ()
Type String Method
- Single Quoted String
- Double Quoted String
- Heredoc Syntax
- Newdoc Syntax (since PHP 5.3)
Single Quoted PHP String
We can create a string in PHP by enclosing text in a single quote. It is the easiest way to specify string in PHP.
<?php
$sVal='Hello Single Quoted String';
echo $sVal;
?>
Output: Hello Single Quoted String
Output: Hello Single Quoted StringDouble Quoted PHP String
Double Quoted PHP StringIn PHP, we can specify string through enclosing text within double quote also. But escape sequences and variables will be interpreted using double quote PHP strings.
<?php
$sDouble="Hello Php within double quote";
echo $sDouble;
?>
Output: Hello Php within double quote
Output: Hello Php within double quoteDouble Quoted PHP String Working Step :
The above example creates a simple string with the value of sDouble.The variable name is then used in the string created using double quotes and its value is interpolated at run time.
In double quoted strings, variable will be interpreted.
<?php
$num=10;
echo "Number is: $num";
?>
Output: Number is: 10
Output: Number is: 10