Coding/Learn/php/php variables and data types
PHP Foundational Course
01.Introduction, Server-Side Architecture & Syntax
02.Variables, Strict Types & Dynamic Casting
03.Strings, Numbers & Built-in Math Functions
04.Operators, Null Coalescing & Magic Constants
05.Control Flow: If-Else, Switch & Modern Match Expressions
06.Loops: While, For & Foreach Traversal
07.Functions, Type Declarations & Named Arguments
08.Arrays & Functional Array Manipulation
09.Superglobals, Form Handling & Input Sanitization
10.Date, Time & DateTimeImmutable Objects
11.File System, Streams & I/O Operations
12.Cookies, Sessions & State Management
13.Exceptions, Throwables & Custom Error Fences
14.Classes, Objects & Constructor Property Promotion
15.Inheritance, Access Modifiers & Method Overriding
16.Interfaces, Abstract Classes & Polymorphism
17.Traits, Static Members & Namespaces
18.PDO, MySQL & Prepared Statements against SQL Injection
19.JSON & XML REST API Interoperability
20.Capstone: Production Secure Backend API & User Registry Engine
Course Certificate0%

Complete all lessons to earn

LearnVariables, Strict Types & Dynamic Casting
2 / 20 0%
Lesson 02 of 20
8 mins read

Variables, Strict Types & Dynamic Casting

PreviousNext Lesson
In Plain English

Variables in PHP start with a dollar sign ($) and are dynamically typed by default. Learn strict type enforcement (declare(strict_types=1)), scalar types (int, float, string, bool), compound types (array, object), and explicit type casting.

The Mental Model

"A variable without strict types is like a flexible cardboard box that can hold shoes, books, or water bottles at any time. Adding strict types turns the box into a molded holder that only accepts one precise shape."

Deep Dive: How It Works

Variable Syntax: Always prefixed with $ (e.g. $userId = 101;).

Scalar Types: int, float, string, bool, and special types null and resource.

Explicit Casting: Prefix expressions with (int), (float), (string), (bool), or (array).

Strict Types: declare(strict_types=1); enables strict argument and return type checking across function calls.

Syntax Blueprint

<?php

$name = "Alice";
$age = (int)"28";
$isActive = true;

Declare variables with $, assign literals, and convert types explicitly with (type) casting.

PointCore Rules to Remember

Point star
$ Prefix Requirement: All variables must start with $ followed by a letter or underscore.
Point star
Type Juggling: PHP automatically coerces strings to numbers in arithmetic operations ("5" + 10 = 15).
Point star
Var Dump Inspection: Use var_dump() to inspect the exact data type, length, and internal value of any variable.

Common Beginner Traps & How to Fix Them

Mistake:Assuming "0" evaluates to true in boolean checks.

Why it happens: In PHP, both "" (empty string) and "0" (string containing zero) evaluate to false in boolean contexts.

How to fix: Use strict comparison ($val === "") or strlen($val) > 0 when checking string input.

Pro Tip: Always declare strict_types=1 at the top of every PHP file in modern applications for robust runtime type safety.

Live Interactive Example

Hit Run Code to see it live
Variable Types and Explicit Type Casting
PHP 8.2 / CLI
1
2
3
4
5
6
7
8
9
10
11
Output Console
Click "Run Code" to view the rendered output.
How it works: (int)$rawScore cast the string "95" to 95, and adding float 4.5 produced 99.5.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Cast and Format User Metrics

  • PointDeclare a variable $rawPrice = "49.99".
  • PointCast $rawPrice to float and store it in $price.
  • PointMultiply $price by 2 and print "Total: $" followed by the result.
1
2
3
4
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates