Link Search Menu Expand Document

Strings in PHP

A string is series of characters. This section will cover how to work with string data in PHP.

Table of Contents

  1. Plain Strings and Fancy Strings
  2. String Length and Concatenation

Plain Strings and Fancy Strings

The most common ways to specify strings is by wrapping characters in single or double quotes.

If a string is specified using double quotes then any variables within curly-braces or escape characters it contains will be evaluated.

<?php
    $name = 'Bobby McGee';
    $fancy_string = "My name is {$name}.\n"; // The \n represents a newline.
    $plain_string = 'My name is {$name}.\n';
    echo $fancy_string;
    echo $plain_string;
?>

Output:

My name is Bobby McGee.
My name is {$name}.\n

Resources

String Length and Concatenation

Strings can be concatenated together using the dot operator.

We can use strlen() to determine the length of a string.

<?php
    $old_shopping_list = 'bacon,chickpeas,gasoline,grapes';
    $new_shopping_list = $old_shopping_list . ',corn,tricycle';
    $output  = "Our list is " . strlen($new_shopping_list);
    $output .= " characters long." // Add to an existing string by using .=
    echo $output;
?>

Output:

Our list is 45 characters long.