PHP BasicsCore· 35 min read

Variables & Data Types

Store data in variables — every PHP variable starts with a dollar sign.

What you will learn

  • Create variables with $
  • Use the main data types
  • Output variables inside strings

Variables start with $

A variable is a labelled box that stores a value so you can use it later. Every PHP variable begins with $, then a name you choose. You do not declare a type — PHP figures out on its own whether you stored text, a number, and so on.

Variables and joining with a dot (.)
<?php
  $name = "Asha";
  $age = 22;
  $price = 99.50;
  $isStudent = true;

  echo "Name: " . $name;       // join with a dot
  echo "<br>Age: " . $age;
?>

The first four lines store values: text in $name, a whole number in $age, a decimal in $price, and a true/false value in $isStudent. The = means "put the value on the right into the box on the left". The two echo lines then glue the label text and the stored value together with a dot . and print them.

Note: Output (in the browser): Name: Asha Age: 22 The dot . joined "Name: " with whatever was inside $name to make one piece of text. The <br> inside the second echo pushed "Age:" onto a new line.

Watch out: PHP joins strings with a dot ., not a plus. "Hi " . $name is correct; "Hi " + $name is not.

Variables inside double-quoted strings

There is a shortcut for joining: inside double quotes, PHP swaps a variable for its value automatically (this is called *interpolation*). Inside single quotes, it does not — you get the variable name as literal text.

Double quotes expand variables; single quotes do not
<?php
  $name = "Asha";
  echo "Hello, $name!";    // Hello, Asha!
  echo 'Hello, $name!';    // Hello, $name!  (single quotes — literal)
?>

Note: Output (in the browser): Hello, Asha!Hello, $name! Double quotes turned $name into Asha. Single quotes printed the characters $name exactly as written — they do not look inside variables. Remember the rule: double quotes read variables, single quotes do not.

TypeExample
String"Hello"
Integer42
Float99.5
Booleantrue / false
Array["a", "b"]
NULLnull

Q. How do you join two strings in PHP?

Answer: PHP concatenates strings with the dot operator: "Hello " . $name.

✍️ Practice

  1. Create variables for your name, age and city, then echo a sentence using them.
  2. Show the difference between double-quote and single-quote variable insertion.

🏠 Homework

  1. Build a “profile” PHP page that stores your details in variables and outputs them as HTML.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →