PHP String

Summary: in this tutorial, you will learn about PHP strings and how to use them 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 simply 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

Suppse you have a a variable called $name:

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

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

Hello John

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

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

However, if you use the double-quoted string, you can simply 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 replace the value of any variable that you place inside the string. This 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 is a good practice to use single-quoted strings because PHP doesn’t have to parse and evaluate them as it does for the double-quoted strings.

Accessing characters in a string

String is zero-based index. To access a character of 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 number of characters in 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 substitude variables embeded in a double-quoted string.
  • A string is 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 return the length of the string.
Did you find this tutorial useful?