Foundation · 40 MINUTES · VERSION 1
PHP values, types, and strict comparisons
Predict mixed-type comparisons, validate minute text, and convert deliberately before strict checks.
Reviewed 23 Oct 2026Your setup may differ. Windows, Ubuntu, and optional tool bundles →
What you will learn
- Distinguish integers, strings, Booleans, and null.
- Predict loose and strict comparisons for numeric text.
- Validate a small text input before converting it to an integer.
Theory: a value has a type
PHP variables start with $. Assignment uses =. An integer such as 30 represents a whole number. A string such as '30' is text. true and false are Boolean values, and null represents no value. The name of a variable should explain its purpose: $minutes is clearer than $x.
== compares with PHP's type-conversion rules. === checks both value and type. In this lesson, the integer 30 and numeric string '30' compare equal with == but not with ===. Decide the intended input type and normalize it deliberately. Strict comparison helps detect unexpected types; it does not validate the meaning or allowed range of input.
declare(strict_types=1) concerns scalar type declarations at function-call boundaries in that file. It does not change == into ===, prevent all conversions, or validate form data. Function type declarations are studied in a later lesson. Do not use this declaration as a replacement for request validation.
Worked example: minutes from text
Required: PHP 8.4 or a compatible newer version. No database, Laravel, or Python is needed to run this program. Save it as php_values.php in your practice folder:
<?php
declare(strict_types=1);
$minutes = 30;
$inputMinutes = '30';
$enabled = false;
$label = 'PHP';
$missing = null;
echo 'Minutes type: '.get_debug_type($minutes).PHP_EOL;
echo 'Input type: '.get_debug_type($inputMinutes).PHP_EOL;
echo 'Loose comparison: '.($minutes == $inputMinutes ? 'true' : 'false').PHP_EOL;
echo 'Strict comparison: '.($minutes === $inputMinutes ? 'true' : 'false').PHP_EOL;
echo 'Enabled: '.($enabled ? 'true' : 'false').PHP_EOL;
echo 'Label: '.$label.PHP_EOL;
echo 'Missing type: '.get_debug_type($missing).PHP_EOL;
// This exercise accepts only whole-minute strings from 0 to 480.
$validFormat = preg_match('/\A(?:0|[1-9][0-9]{0,2})\z/', $inputMinutes) === 1;
if (! $validFormat || (int) $inputMinutes > 480) {
echo 'Input error: use whole minutes from 0 to 480.'.PHP_EOL;
exit(1);
}
$validatedMinutes = (int) $inputMinutes;
echo 'Validated minutes: '.$validatedMinutes.PHP_EOL;
echo 'Strict after conversion: '.($minutes === $validatedMinutes ? 'true' : 'false').PHP_EOL;
Run php php_values.php from the practice folder. This command is shared by Windows PowerShell/Command Prompt and Ubuntu Bash when PHP is on PATH. See /getting-started if you need to select your installed executable. Laragon/XAMPP are optional local tools; the source code is the same on all these systems.
Expected output:
Minutes type: int
Input type: string
Loose comparison: true
Strict comparison: false
Enabled: false
Label: PHP
Missing type: null
Validated minutes: 30
Strict after conversion: true
get_debug_type makes the type visible. The expressions inside parentheses choose a printable true or false string. This avoids the confusing result of directly printing a false Boolean, which appears as an empty string. A dot joins text. PHP_EOL ends the line appropriately for the local system.
The format check is a small regular expression, a pattern for text. It accepts '0' or one to three digits with no leading zero. The anchors require the whole input to match. The second check rejects a number above 480. The rule deliberately rejects spaces, a sign, decimals, and leading zeros. This is a policy for this exercise, not the only valid application policy. In a Laravel request, use the framework's validation rules with an explicit input contract.
The explicit (int) conversion happens only after these checks. Casting alone is not validation: a value such as '30minutes' must be rejected by this rule even if conversion might produce a number. The example assumes the input variable is a string; real decoded JSON can contain arrays, null, or numbers and needs a type check before string-processing functions.
Guided practice
- Run the original example and compare all nine output lines.
- Change
$inputMinutesto'0'. It is valid. Expect validated minutes 0 and a false final comparison with the fixed$minutes = 30. - Try
'480'and then'481'. The first passes; the second prints the input error and exits with code 1. - Try
'','03','-1','30.5',' 30 ', and'30minutes'. All fail this exercise's rule. Predict each result before running it. - To check the last program exit code, use
$LASTEXITCODEin Windows PowerShell,echo %ERRORLEVEL%in Command Prompt, orecho $?in Ubuntu Bash. Zero means this program accepted the input; one means its explicit validation failure. Exit codes do not measure learning progress.
Independent exercise
Create a similar program for a daily task target. Accept whole-number strings from 0 to 20, with the same no-space/no-leading-zero policy. Compare the validated target strictly with the integer 5. Print a clear error and exit 1 for invalid values. Test '0', '5', '20', '21', '05', and 'five'.
Write a table with input text, expected validity, actual validity, and the final type. Do not change the input rules silently to make a test pass. Explain why a zero target is valid under your chosen rule even though it may not be a useful goal.
Common mistakes
- Using
=in place of a comparison: assignment changes a variable. - Assuming
'false'is a false Boolean: it is a non-empty string; normalize documented input values deliberately. - Assuming strict mode changes operators:
==still uses its own comparison rules. - Treating
null,false, zero, and empty text as the same concept: choose the exact meaning required by the application. - Casting unchecked text to a number: check format, type, and allowed range first.
Self-check — not graded
- Is
'30' === 30true? - Does strict comparison check a maximum allowed study time?
- What does this lesson's validation do with
'03'?
Interview practice
“Why use strict comparison?” Explain a concrete mixed-type example, normalization at the input boundary, and strict checks afterward. Mention that validation, authorization, and business rules remain separate responsibilities.
English practice and glossary
- Integer: a whole-number type.
- String: text.
- Boolean: a true/false type.
- Null: no value.
- Conversion: deliberately changing a value's type.
- Range: the minimum and maximum allowed values.
Write: “The input is a string. I checked __ before converting it. The value __ was rejected because __.” Explain a failing case aloud with its actual output.
Recap and next step
Check the incoming type, validate the rule, convert deliberately, then compare with the intended type. Next, use PHP arrays and loops to build a small task report. Sources and review dates are listed below.
Reveal exercise hints and self-check answers
- No. The types differ.
- No. Range validation is a separate check.
- It rejects the leading zero under this exercise's explicit policy.
Independent exercise outline: reuse the format pattern, change the upper bound to 20, convert only after validation, and compare with integer 5 using ===. The first three test values are valid; '21', '05', and 'five' are invalid. The validated value's type is int. Review both expected output and exit code. A Boolean false, integer zero, and null can have different meanings even when loose checks hide that distinction.