Real World PHP

About   |   EN   |   ES

006d Data Types: Strings

A string is a chain of characters. The use of the term “string” dates back to the 1960’s when early programmers had need to represent a string of letters or characters in their code. It is worthwhile to search the internet for the etymology of the term with respect to computer programming.

In PHP, each character in a string is represented by one byte. In other words, PHP does not natively support multi-byte characters in strings (Unicode). That does not mean you cannot use Unicode, but rather, in order to use Unicode, you will need to rely on special functions to do so. See the official documentation for more details on handling Unicode in PHP.

There are four ways to define a string in PHP:

<?php
// Double quotes:
$string = "This is a string example. It's great!";

// Single quotes:
$string = 'Another example of a string. :)';

// HEREDOC (funny name, but it is what it is)
$string = <<<EOT
You can put anything here. You
    can indent over multiple lines.
  Note that a heredoc begins with three "<" symbols and
  an identifier (in this case EOT, but the identifier 
  can be anything).
The HEREDOC ends with the identifier and a semicolon.
EOT;

// NOWDOC (similar to a HEREDOC)
$string = <<<'EOT'
A NOWDOC is similar to a string in single-quotes. A HEREDOC is
similar to a string in double-quotes. 
Note how the identifier for a NOWDOC is surrounded with single-quotes
EOT; 

Single quote strings

Using single quotes is the simplest way to create a string. Whatever you put inside the quotes will be the string. There are two exceptions to this. If you want a single quote inside of a single-quoted string, it must be escaped with a backslash. Also, if you want to have a backslash, it must be escaped with a backslash.

<?php
// Single quote string with a single quote inside it.
$string = 'This string has \'single\' quotes.';

// Single quote string with a backslash in it.
$string = 'The file is in the C:\\ drive.'; // Will output only one "\" character

Double quote strings

Double quote strings allow for special escaped characters and do variable parsing.

There is a complete list of escaped characters in the official documentation, but here are a few commonly used:

When PHP parses a double quote string, it checks to see if there are any variables within the string and resolves them first.

<?php
// Example of a newline escaped character
echo "Real\nWorld\nPHP!"; // Outputs:
// Real
// World
// PHP!


// Variable parsing
$name = "John Doe";
echo "The name is $name."; // Outputs:
// The name is John Doe.

// Second method of variable parsing
$name = "John Doe";
echo "The name is {$name}."; // Outputs:
// The name is John Doe.

HEREDOC strings

Just like double quoted strings, HEREDOC strings are parsed for escaped characters and variables by PHP. The exception is that double quotes do not need to be escaped, although they can be.

NOWDOC strings

NOWDOC strings are similar to single quoted strings, but they do not parse any escaped characters.

String Access

You can access any character within a string by using brackets. The first character in a string has an index of 0, the second has an index of 1, and so forth for the length of the string.

<?php
$string = "Hello";
echo $string[0]; // Output: H
echo $string[2]; // Output: l

String Concatenation

Concatenation is just putting two strings together. It is done using the dot(.) operator. Some languages use the plus (+) operator to do this, but that will not work in PHP.

<?php
$string1 = "Hello";
$string2 = "World";
echo $string1 . $string2; // Output: HelloWorld
echo $string1 . " " . $string2; // Output: Hello World

Casting to string

You can cast to a string by using the strval() function or by using (string). Some things convert as expected, but some do not.

<?php
// Boolean to string
echo (string) true; // Output: 1
echo (string) false; // Does not output anything! It casts to an empty string: ""

// Int to string
echo (string) 3; // Output: 3

// Float to string
echo (string) 3.1415; // Output: 3.1415

// Array to string
echo (string) [1,2,3]; // Output: PHP Warning: Array to string conversion...
// Casting an array to a string returns "Array" and a warning.

// Object to string
echo (string) (new stdClass()); // Output: PHP Warning: Uncaught error...
// Casting an object to a string returns a warning.
// In order to cast an object, the magic __toString() method must
// be defined on the object. More on this later when we get to 
// OOP and magic methods.

// null to string
echo (string) null; // Does not output anything. It casts to an empty string: ""

Best Practices

Strings and string manipulation are an integral part of most programming languages, and PHP is no exception. As a consequence, there are many opinions on how to handle strings. We encourage reading the documentation, experiment, and decide for yourself. Additionally, take care when someone says that using single quotes is better. And remember that Unicode is complicated and needs to be well understood before making assumptions about how it will behave with your code.


Resources


Challenges

Concatenation

Practice concatenating strings.

First, use one echo statement to display these three strings together:
"I came."
"I saw."
"I conquered."

Next, assign each of those strings to a variable and then use one echo statement to display the three strings together in one line.

Max string length

Search the internet/the documentation to learn how long one string can be in PHP.