Creating and working with Strings in PHP
  John Mwaniki /   06 Sep 2021

Creating and working with Strings in PHP

What are Strings?

A string is a data type made up of a sequence of characters, either as a variable or a literal constant.

It can comprise of letters, words, phrases, numbers, or symbols. For example, the word "programming" and the phrase "I love programming" are both strings. "12345678" if specified correctly, or random characters such as "asd&@1234(*#" are considered a string.

Typically, the string content needs to be enclosed within quotes for it to be considered a string.

String Specification

Strings in PHP can be specified in 4 different ways:

  • Single quoted
  • Double quoted
  • Heredoc syntax
  • Nowdoc syntax

1. Single quoted

This is the easiest way to specify strings in PHP. It involves enclosing text within single quotes (').

Example

<?php
$mystring = 'This is a string';
echo $mystring; 
//Output: This is a string

This type of string does not process variables and special characters inside the quotes.

Example

<?php
$name = 'John';
$mystring = 'My name is $name\n';
echo $mystring; 
//Output: My name is $name\n

In the above example, the echo statement prints the variable name instead of replacing it with its content. This is because this type of variable is unable to identify the ‘$’ sign as the start of a variable name.

To include a variable in a single-quoted string, simply break it at the position where the variable appears and concatenate it with the variable being outside of the quotes.

Example

<?php
$name = 'John';
$occupation = 'software developer';
$mystring = 'My name is '.$name.'. I am a '.$occupation.'.';
echo $mystring; 
//Output: My name is John. I am a software developer.

You will need to use a backslash (\) to escape a literal single quote within a string. To specify a literal backslash (\) use double backslash (\\). All other instances with a backslash such as \r or \n, will be output literally as specified rather than having any special meaning.

Example

<?php
$mystring = 'I can\'t get bored with coding\\programming';
echo $mystring; 
//Output: I can't get bored with coding\programming

 

2. Double quoted

This involves enclosing the text within double quotes ("). Unlike the single quoted strings, special characters and variables are interpreted and processesd withing double quoted strings.

Example

<?php
$name = "John";
$occupation = "software developer";
echo "My name is $name\n.";
echo "I am a $occupation."; 
/*
  Output:
  My name is John.
  I am a software developer.
*/

From the above example, the variables are replaced with their respective values and the new line (\n) escape character interpreted for a line break.

3. Heredoc Syntax

The heredoc is a string method for multi-line strings. Strings using the heredoc syntax are defined by having the heredoc operator “<<<” followed by an identifier(can be any text), and then immediately followed by a newline. The string itself follows, and then the same identifier again to close the quotation.

Example:

<?php
$message = <<<EOD
Sample paragraph1
EOD;

The closing identifier must begin in the first column of the line(ie. no whitespace or tab before it). The line with the closing identifier must contain no other characters, except a semicolon (;), ie, make sure that there is no space before EOD; or after it.

Heredoc syntax is similar to double-quoted string without quotes.

4. Nowdoc Syntax

Newdoc is very similar to heredoc, but with a different parsing. Its syntax is very similar to that of heredoc but its identifier is enclosed in single quotes.

Example

<?php
$mystring = <<<'ABCD'
Sample paragraph1
'ABCD';

The nowdoc syntax is similar to the single-quoted string.

Conclusion

In this article, you have learned what a string is in PHP and all the different ways in which you can specify it.