typestar

String interpolation in PHP

Double quotes splice variables into strings; single quotes leave them alone.

<?php
$name = "typestar";
$letters = 8;

// double quotes interpolate; single quotes stay literal
echo "$name has $letters letters\n";
echo 'literal: $name has $letters letters', "\n";

// braces mark off array lookups and other expressions
$langs = ["php", "ruby"];
echo "first up: {$langs[0]}\n";
echo "shouted: " . strtoupper($name) . "\n";

How it works

  1. "$name has $letters letters" interpolates both variables in place.
  2. The single-quoted twin prints the dollar signs literally.
  3. Braces as in {$langs[0]} mark off array lookups inside a string.
  4. . concatenates when the value comes from a call like strtoupper.

Keywords and builtins used here

The run, in numbers

Lines
12
Characters to type
343
Tokens
66
Three-star pace
70 tpm

At the three-star pace of 70 tokens a minute, this run takes about 57 seconds.

Type this snippet

Step 2 of 3 in Variables & types, step 2 of 27 in Language basics.

← Previous Next →

String interpolation in other languages