Strings and String Functions

Learn PHP /Fundamentals /Lesson 3 of 12 /PHP 8.3 in your browser
In this lesson you’ll learn to
  • Build sentences by dropping variables into double-quoted strings
  • Clean up and transform text with built-in functions

Text is the type you will reach for most: names, messages, page titles, everything a person reads. In PHP a piece of text is called a string, and PHP gives you a large toolbox for shaping them. This lesson covers the essentials you will use every day.

This builds on Variables and Data Types, where you met the string type.

Making a String

A string is any text wrapped in quotes. Both single quotes and double quotes create a string, and for plain text they behave the same way:

<?php
$single = 'Hello';
$double = "Hello";
echo $single;
echo " and ";
echo $double;
Hello and Hello

So far, no difference. But the two quote styles are not interchangeable, and the difference between them is one of the most useful things to understand about PHP.

Single vs Double Quotes

The rule is short: double quotes look inside the string for variables and swap in their values; single quotes do not. Swapping a variable’s name for its value is called interpolation.

Single quotes ‘ ‘

Printed exactly as typed. A variable name stays as plain text.

$name = "Billy";
echo 'Hi $name';
// Hi $name
Double quotes ” “

Variables are swapped for their value.

$name = "Billy";
echo "Hi $name";
// Hi Billy

Flip the switch and run it both ways:

<?php
$name = "Billy";
echo 'Hi $name';
Hi $name
<?php
$name = "Billy";
echo "Hi $name";
Hi Billy

When a variable sits right up against other letters, wrap it in curly braces so PHP knows where the name ends: "{$name}s". As a rule of thumb, use single quotes when you want a literal $ in the output, and double quotes when you want to drop a value into a sentence.

Joining Strings Together

To stick two strings together, use the dot (.). This is called concatenation. It is easy to confuse with the plus sign from math, so make a prediction first:

What does this print?

<?php
echo "5" . "5";
  • 55
  • 10
  • 5 5

The dot joins the two strings end to end, giving “55”. A plus sign would add them as numbers to make 10, but the dot never does math and never adds a space.

You can also add to a string you already have with .=, which appends to the end:

<?php
$message = "Loading";
$message .= "...";
echo $message;
Loading...

Handy String Functions

PHP ships with hundreds of string functions. These few will cover most of your day-to-day work. Click through each one and read the result:

<?php
echo strlen("Hello");
5
<?php
echo strtoupper("hello");
HELLO
<?php
echo strtolower("HELLO");
hello
<?php
echo substr("Hello world", 0, 5);
Hello
<?php
echo ucfirst("hello");
Hello
<?php
echo trim("  hello  ");
hello
<?php
echo str_replace("world", "PHP", "hello world");
hello PHP
<?php
var_dump(str_contains("hello world", "world"));
bool(true)

strlen() counts bytes, which is the same as characters for plain ASCII like "Hello" but not for multibyte UTF-8 text: an accented letter or an emoji is one character made of several bytes, so strlen("café") reports 5, not 4. For text a person might type in any language, use mb_strlen() to count actual characters. strtoupper(), strtolower(), and ucfirst() change case, substr() cuts out a piece of a string by position and length, and str_contains() answers a yes-or-no question with a boolean. trim() is especially useful for cleaning up text that people type, where a stray space at the start or end is easy to leave behind. PHP also has sprintf() and heredoc syntax for building longer strings from a template; you will meet them later in the course.

Escape Sequences

Some characters are awkward to type inside a string. A backslash lets you escape them. These work inside double quotes:

  • \n is a new line, and \t is a tab
  • \" is a literal double quote, and \\ is a literal backslash
<?php
echo "Line one\nLine two";
Line one
Line two

Single quotes are more literal: inside them only \' and \\ do anything, so '\n' prints a backslash and an n, not a new line.

Quoting a quote. If your text contains the same quote you wrapped it in, either escape it with a backslash or switch quote styles. Both "She said \"hi\"" and 'She said "hi"' work.

Check Yourself

You have $name = "Sam";. Which line prints Hello Sam?

  • echo “Hello $name”;
  • echo ‘Hello $name’;
  • echo “Hello” $name;

Only double quotes interpolate the variable. Single quotes print the literal text “Hello $name”, and the third line is missing the dot needed to join the two pieces.

Your Turn

The variable $name holds "billy" in lowercase. Print it with a capital first letter, so the output is exactly Billy. One of the functions above does this in a single step. Then hit Check my answer.

<?php
$name = "billy";
<?php
$name = "billy";
echo ucfirst($name);

What You Learned

  • Strings are text in quotes; double quotes interpolate variables, single quotes stay literal
  • The dot (.) joins strings, and .= appends to one you already have
  • Functions like strlen, strtoupper, ucfirst, trim, and str_replace shape text

You can now:

  • Build sentences by dropping variables into double-quoted strings
  • Clean up and transform text with built-in functions

Text often travels with numbers: prices, totals, counts. Next you will work with numbers and math.

Comments

Leave a reply

You’ll learn to
  • Build sentences by dropping variables into double-quoted strings
  • Clean up and transform text with built-in functions
Next: Numbers and Math →