Variables
On this page
Creating Variables
Variables start with $. PHP is dynamically typed, so the type can change based on assigned value.
<?php $age = 12; // int $price = 19.99; // float $name = 'Ozan'; // string $isActive = true; // bool
Type Juggling (Be Careful)
PHP can automatically convert types in some operations. This is powerful, but can create bugs if you assume strict types everywhere.
<?php echo 10 + '5'; // 15 (string '5' becomes number) echo '10' . 5; // '105' (string concatenation)
Null Coalescing for Defaults
Use ?? to set safe defaults when a value may be missing (common for request data).
<?php $q = $_GET['q'] ?? ''; $page = (int)($_GET['page'] ?? 1);
Variable Naming Conventions
Use descriptive names and consistent style (e.g., camelCase for variables). Avoid cryptic names like $x unless it's a short loop variable.
<?php $totalPrice = 0; $itemsCount = 0;
Debugging Variables
During development, you can inspect values using var_dump or print_r. Don't expose these in production responses.
<?php var_dump($page);