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); // stringConstants
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 dotOperators
$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 coalescingTip: 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.