Lesson 2 / 10

Variables, Data Types and String Interpolation

Every PHP variable starts with a dollar sign, and — like Python and JavaScript, unlike Java or C++ — you never declare its type up front. PHP figures the type out automatically from whatever value you assign.

$age = 25;            // intn$price = 19.99;       // floatn$name = "Tutoline";   // stringn$isActive = true;     // bool

Core types

  • int, float — numbers
  • string — text
  • bool — true/false
  • array — lists and key/value maps, covered fully in the next lesson
  • null — explicitly no value

String interpolation

$name = "Yash";necho "Hello, $name!";                 // works inside double-quoted stringsnecho 'Hello, $name!';                 // prints literally: Hello, $name!

PHP substitutes variables inside double-quoted strings automatically — but not inside single-quoted ones, where $name is treated as plain literal text. This is a common source of bugs for anyone switching between the two quote styles without noticing, so keep the distinction firmly in mind as you write PHP.

Curly-brace interpolation

$student = ["name" => "Yash"];necho "Hello, {$student['name']}!";   // curly braces needed for array access inside a string

Simple variables interpolate directly, but accessing an array element or an object property inside a double-quoted string usually needs curly braces around the whole expression to interpolate correctly and unambiguously.

Type juggling

$total = "5" + 3;      // 8 -- PHP converts the numeric string automaticallyn$label = 5 . " items"; // "5 items" -- the dot operator concatenates strings

PHP is often described as “loosely typed” or “type juggling” — it converts between types automatically in many situations, which can feel convenient at first but has caused real, well-documented bugs across the PHP ecosystem historically. Modern PHP code increasingly opts into strict typing with declare(strict_types=1); at the top of a file to avoid this automatic conversion entirely.