0
0
PHPprogramming~10 mins

Union types in practice in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Union types in practice
Function with Union Type Parameter
Call function with int
Accept int
Call function with string
Accept string
Call function with other type
Error: Type Mismatch
Function returns value
The function accepts either int or string as input. It checks the type and processes accordingly. If input is not int or string, it causes a type error.
Execution Sample
PHP
<?php
function printValue(int|string $value): void {
  echo "Value: $value\n";
}
printValue(10);
printValue("hello");
printValue(3.14); // error
?>
This code defines a function that accepts either an int or a string and prints it. It is called with int, string, and a float (which causes an error).
Execution Table
StepFunction CallParameter TypeAccepted?Output or Error
1printValue(10)intYesValue: 10
2printValue("hello")stringYesValue: hello
3printValue(3.14)floatNoTypeError: Argument 1 passed to printValue() must be of type int|string, float given
💡 Execution stops at step 3 due to TypeError caused by passing float instead of int|string.
Variable Tracker
VariableStartAfter Call 1After Call 2After Call 3
$valueundefined10 (int)"hello" (string)3.14 (float) - error
Key Moments - 2 Insights
Why does passing 3.14 cause an error even though it is a number?
The function only accepts int or string as per the union type int|string. Float is not allowed, so step 3 in the execution_table shows a TypeError.
Can the function accept both int and string without separate overloads?
Yes, union types let the function accept either type in one definition, as shown in steps 1 and 2 where both int and string inputs are accepted.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output when printValue is called with "hello"?
AValue: hello
BValue: 10
CTypeError
DNo output
💡 Hint
Check step 2 in the execution_table where printValue("hello") is called.
At which step does the function call cause a TypeError?
AStep 1
BStep 3
CStep 2
DNo error
💡 Hint
Look at the 'Accepted?' and 'Output or Error' columns in the execution_table.
If the function parameter type was changed to int|float, what would happen at step 3?
AIt would accept only int
BIt would cause a TypeError
CIt would accept 3.14 and print it
DIt would accept string too
💡 Hint
Consider the union type change and how it affects the accepted parameter types.
Concept Snapshot
Union types allow a function parameter or return to accept multiple types.
Syntax: function f(int|string $param): void
The function accepts either int or string.
Passing other types causes a TypeError.
Useful for flexible but type-safe code.
Full Transcript
This example shows a PHP function using union types to accept either an int or a string parameter. The function printValue prints the value passed. When called with 10 (int) or "hello" (string), it works fine and prints the value. When called with 3.14 (float), it causes a TypeError because float is not part of the union type int|string. The execution table traces each call, showing accepted types and outputs or errors. The variable tracker shows how the parameter changes each call. Key moments clarify why float causes an error and how union types allow multiple accepted types in one function. The quiz tests understanding of outputs, error steps, and effects of changing union types. The snapshot summarizes union types as a way to accept multiple types safely in PHP functions.