Concept Flow - Type casting syntax
Start with a variable
Apply type cast syntax
Variable changes type
Use the new typed variable
End
This flow shows how a variable is changed from one type to another using PHP's type casting syntax.
<?php $var = "123"; $intVar = (int)$var; echo $intVar; ?>
| Step | Action | Variable | Value Before | Type Before | Value After | Type After | Output |
|---|---|---|---|---|---|---|---|
| 1 | Initialize variable | $var | undefined | undefined | "123" | string | |
| 2 | Apply (int) cast | $intVar | undefined | undefined | 123 | integer | |
| 3 | Print $intVar | $intVar | 123 | integer | 123 | integer | 123 |
| 4 | End | - | - | - | - | - | - |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|---|
| $var | undefined | "123" | "123" | "123" | "123" |
| $intVar | undefined | undefined | 123 | 123 | 123 |
PHP Type Casting Syntax: - Use (type) before a variable to convert it. - Common types: (int), (float), (string), (bool), (array), (object). - Casting creates a new value; original variable unchanged. - Example: $intVar = (int)$var; - Useful to ensure variable is the expected type.