Learn PHP from Scratch

PHP Variables, Data Types and Operators

Lesson 2 of 7 1 min read Updated 28 September 2026

Variables

PHP variables start with a dollar sign and do not need a type declaration.

<?php
$name = "Asha";
$age = 20;
$price = 99.5;
$isStudent = true;

echo "Name: $name, Age: $age";
echo 'Single quotes do not parse $name';

Double quotes parse variables, and single quotes do not.

Data types

  • string, int, float, bool
  • array, object
  • null
var_dump($age);          // int(20)
var_dump($price);        // float(99.5)
echo gettype($name);     // string

Constants

const SITE = "LaunchCV";
define("YEAR", 2025);
echo SITE . " " . YEAR;

Strings

$text = "Hello World";
echo strlen($text);              // 11
echo strtoupper($text);          // HELLO WORLD
echo str_replace("World", "PHP", $text);
echo substr($text, 0, 5);        // Hello
echo "Hi " . $name . "!";        // concatenation with a dot

Operators

$a = 10; $b = 3;
echo $a + $b;   // 13
echo $a % $b;   // 1
echo $a ** 2;   // 100
echo $a <=> $b; // 1 (spaceship: -1, 0 or 1)
echo $a === "10" ? "same" : "different";  // different (strict)
$user = $_GET['user'] ?? 'Guest';         // null coalescing

Tip: Use === for comparisons. It checks both value and type, which prevents subtle bugs.

Practice

Store a student's name and three marks in variables, calculate the total and average, and print a sentence with the result.