Strings make an essential token in a programming language. They can be termed as a collection of characters identified as a single unit. A string can be assigned to a variable. It can also be printed as a message after concatenating with the calculated values, returned from or passed to functions as arguments. PHP string specification is required to present data processed by a web server on web pages in form of formatted text.

PHP String Specification

PHP Strings can be specified by enclosing the set of characters in single quotes(‘ ‘) or double quotes (“”). The following are the valid string values.

$emp_name=’Bob Stuart’;
$messgae=”Hello world!”;

String in Single Quotes

When you give PHP String Specification in single quotation marks the strings are taken as it is. No interpolation or parsing of variables, if included, in the strings is done. Consider the following code

<?php
$my_age=30;
echo  'Hello, my name is Susan and my age is $my_age';
?>

The output will be

Hello, my name is Susan and my age is $my_age

In the above output the variable $my_age is not replaced by its assigned value since the string is in single quotes.

By using a pair of backslashes or a backslash and a single quote  in a singly quoted string, you can print a backslash and a single quote respectively  in output. These escape sequences are not taken literally in single quote strings.

<?php
echo  'Susan\'s and my home are across the slash \\';
?>

The output will be

Susan's and my home are across the slash \

Here the backslash in singly quoted string defines the escape sequence and prints single  quote and backslash in output.

String in Double Quotes

When you specify strings in double quotation marks they are the preprocessed to evaluate any variables or escape sequences occurring in string in double quotes. The variables in the string are parsed and are replaced with values they hold. Consider the following code

<?php
$my_age=30;
$my_name='Susan';
echo  "Hello, my name is $my_name and my age is $my_age";
?>

The output will be

Hello, my name is Susan and my age is 30

The escape sequences that work in doubly quoted strings are

Escape sequence Replaced by character representing
\n Newline
\r Carriage return
\t tab
\$ Dollar symbol
\” Double quote
\\ Single backslash

 

When variables are included in a doubly quoted string, the $ symbol without escape sequence (\) is considered to be part of a variable name and is parsed according to following rules:

  • A variable with an assigned string value is replaced by its value.
  • A variable having an numeric or Boolean value is first converted to equivalent string that replaces the variable in doubly quoted string.
  • A variable just declared but not assigned any value displays a blank space in output string

Consider the following example

<?php
$strVar=' Hello users';
$numVar=56.3400;
$blankVar;
echo  " This is the string $strVar, This is the numeric $numVar and this is not yet set $blankVar ! Now you understand";
?>

The output is generated with a notice message

Notice: Undefined variable: blankVar in C:\wamp\www\test\test.php on line 5
This is the string Hello users, This is the numeric 56.34 and this is not yet set ! Now you understand