Skip to content

Chapter 4 of 38

Variables and Data Types

Use scalars, arrays, objects, resources, null, conversion, and type inspection.

34 minutes 10 quick checksBy Subha Prasad
Lesson 4 of 38Course navigation

Lesson content

Read, practise, then check your understanding

Variables begin with $ and are case-sensitive. Values include bool, int, float, string, array, object, resource, callable, iterable, and null. Runtime types can change unless declarations constrain them.

Practical example

<?php
$count = 7;
$ratio = 0.75;
$active = true;
$items = ['forms', 'PDO'];
if (!is_int($count)) { throw new TypeError('integer required'); }

Use get_debug_type for diagnostics and is_* predicates for contracts. Type juggling can surprise comparisons and arithmetic, so validate external data and prefer strict equality. Nullable, missing, empty, and invalid are different domain states.

Knowledge check

Answer every question correctly to complete this chapter.

Which statement best describes variable?
Which PHP term matches this description: A dollar-prefixed case-sensitive name bound to a value.
Which statement best describes scalar type?
Which PHP term matches this description: A bool, int, float, or string value.
Which statement best describes compound type?
Which PHP term matches this description: An array or object value containing structure.
Which statement best describes null?
Which PHP term matches this description: The value representing no value.
Which statement best describes type juggling?
Which PHP term matches this description: Automatic conversion between types according to operation context.

0 of 10 checks passed

Your progress is saved on this device.