PHP String

Summary: in this tutorial, you will learn about PHP strings and how to manipulate strings effectively.

Introduction to PHP strings

In PHP, a string is a sequence of characters. PHP provides you with four ways to define a literal string, including single-quoted, double-quoted, heredoc syntax, and nowdoc syntax. This tutorial focuses on the single-quoted and double-quoted strings.

To define a string, you wrap the text within single quotes like this:

<?php
    
$title = 'PHP string is awesome';Code language: HTML, XML (xml)

Or you can use double quotes:

<?php
    
$title = "PHP string is awesome";Code language: HTML, XML (xml)

However, you cannot start a string with a single quote and ends it with a double quote and vice versa. The quotes must be consistent.

Single-quoted strings vs. double-quoted strings

Suppose you have a variable $name.

<?php

$name = 'John';Code language: HTML, XML (xml)

And you want to show a message that displays the following:

Hello John

To do it, you can use the concatenate operator (.) to concatenate two strings:

<?php

$name = 'John';
echo 'Hello ' . $name;Code language: HTML, XML (xml)

However, if you use a double-quoted string, you can place the $name variable inside the string as follows:

<?php

$name = 'John';
echo "Hello $name";Code language: HTML, XML (xml)

When evaluating a double-quoted string, PHP replaces the value of any variable that you place inside the string. This feature is called variable interpolation in PHP.

An alternative syntax is to wrap the variable in curly braces like this:

<?php

$name = 'John';
echo "Hello {$name}";Code language: HTML, XML (xml)

The output is the same.

Note that PHP doesn’t substitute the value of variables in the single-quoted string, for example:

<?php

$name = 'John';
echo 'Hello {$name}';Code language: HTML, XML (xml)

The output will be like this:

Hello {$name}Code language: PHP (php)

Besides substituting the variables, the double-quoted strings also accept special characters, e.g., \n, \r, \t by escaping them.

It’s a good practice to use single-quoted strings when you don’t use variable interpolation because PHP doesn’t have to parse and evaluate them for double-quoted strings.

Accessing characters in a string

A string has a zero-based index. It means that the first character has an index of 0. The second character has an index of 1 and so on.

To access a single character in a string at a specific position, you use the following syntax:

$str[index]Code language: PHP (php)

For example:

<?php

$title = 'PHP string is awesome';

echo $title[0];Code language: HTML, XML (xml)

Output:

P

Getting the length of a string

To get the length of a string, you use a built-in function strlen(), for example:

<?php

$title = 'PHP string is awesome';
echo strlen($title);Code language: HTML, XML (xml)

Summary

  • A string is a sequence of characters surrounded by single quotes or double quotes.
  • PHP substitutes variables embedded in a double-quoted string.
  • A string is a zero-based index. Therefore, you can access a character at a specific position in a string using the square brackets [].
  • Use the strlen() function to get the length of the string.
Did you find this tutorial useful?