0
0
PHPprogramming~10 mins

Union types in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Union types
Function with Union Type
Call function with value
Check if value matches any type
Run function
Return result
The function accepts values of multiple types. It checks the input type and runs if it matches any allowed type, else it errors.
Execution Sample
PHP
<?php
function example(int|string $value): string {
  return "Value is: $value";
}

echo example(10);
echo example("hello");
This code defines a function that accepts either an int or a string and returns a string showing the value.
Execution Table
StepFunction CallParameter TypeType Check ResultActionOutput
1example(10)intMatches int|stringFunction runsValue is: 10
2example("hello")stringMatches int|stringFunction runsValue is: hello
3example(3.14)floatDoes not match int|stringType error thrownFatal error
💡 Execution stops on type error when input does not match any union type.
Variable Tracker
VariableStartCall 1Call 2Call 3
$valueundefined10 (int)"hello" (string)3.14 (float)
Key Moments - 2 Insights
Why does example(3.14) cause an error?
Because 3.14 is a float and the function only accepts int or string, as shown in execution_table row 3.
Can the function accept both int and string in the same call?
No, each call passes one value. The union means the value can be either int or string, not both at once.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output when calling example(10)?
A"Value is: hello"
BFatal error
C"Value is: 10"
DNo output
💡 Hint
Check row 1 in the execution_table for output of example(10).
At which step does the type check fail?
AStep 3
BStep 2
CStep 1
DNo failure
💡 Hint
Look at the 'Type Check Result' column in execution_table.
If the function accepted float as well, what would happen at step 3?
AOutput is empty string
BFunction runs and outputs value
CType error still thrown
DProgram crashes
💡 Hint
If float was added to union, step 3 would match and function would run.
Concept Snapshot
Union types allow a function parameter or return to accept multiple types.
Syntax: function foo(int|string $param): string
The function runs if input matches any type in the union.
If input type does not match, a type error occurs.
Useful for flexible but type-safe code.
Full Transcript
This example shows how PHP union types work. The function example accepts either an int or a string as input. When called with 10 (an int), it runs and returns 'Value is: 10'. When called with 'hello' (a string), it runs and returns 'Value is: hello'. But when called with 3.14 (a float), it causes a type error because float is not in the union. The execution table traces each call, showing the parameter type, type check result, action, and output. The variable tracker shows how the input variable changes with each call. Key moments clarify why the error happens and that the function accepts one type per call. The quiz tests understanding of outputs and type checks. The snapshot summarizes union types as a way to accept multiple types safely in PHP functions.