Codectionary / Developer documentation / PHP

PHP Variables & Strings

PHP variables start with $. Double-quoted strings interpolate variables, while single-quoted strings generally treat their contents literally.

About PHP

PHP is a programming language commonly used on web servers. It can generate HTML, handle submitted forms and work with databases to power dynamic websites and content-management systems.

  • Dynamic websites
  • Content management
  • Server APIs
Created by
Rasmus Lerdorf
First released
1995 · first public release
Version / standard
PHP 8.5

Current stable release line. Hosting providers may offer several supported versions.

In the real world

  • WordPress: The WordPress content-management system
  • Wikipedia: The MediaWiki platform behind the site
  • Drupal: The Drupal content-management system

Facts checked 8 September 2026. Links open the sources; examples describe specific products or documented uses.

Syntax

$name = "Ada";
echo "Hello, $name";

Examples

A small working example

Follow the values through this example, then change one input.

<?php
$name = "Ada";
$lessons = 3;
echo "$name completed $lessons lessons\n";

Printing a greeting

A small, runnable example of this syntax.

<?php
$name = "Ada";
$count = 3;
echo "$name has $count messages.";
?>

Best practices

  • Use descriptive variable names and keep presentation templates separate from application logic as a project grows.
  • Keep examples small while learning, then combine the idea with a real project.

At a glance

Purpose
Server-side web applications
File extension
.php
Runs in
PHP runtime on a server or CLI
Usually used with
HTML, databases and web servers

In plain English

PHP variables start with $. A variable can hold text, numbers or other values, and echo writes output.

What you’ll learn

  • Assign a variable.
  • Read a case-sensitive name.
  • Interpolate text.

Breaking down the syntax

$name
A variable identifier.
=
Assignment.
echo
Writes one or more strings.

How it works

Input or value

Obtain the data.

Variable

Assign it to a meaningful name.

Output

Format the result for its destination.

When should I use this?

Name values in server-side request handling or command-line scripts.

Common mistakes

A common trap

Variable names are case-sensitive.

Incorrect

$name = "Ada";
echo $Name;

Corrected

$name = "Ada";
echo $name;

Compare approaches

  • Single-quoted string: Mostly literal text.
  • Double-quoted string: Variable interpolation and escape sequences.

Explore deeper

Output depends on context

When inserting untrusted text into HTML, encode it for the HTML context. Plain echo does not automatically make user input safe for a web page.

Specifications & further reading

Related PHP documentation