Casting, Math & Constants
Casting converts one type to another. Use (int), (float), (string), (bool), or functions like intval(), floatval(), strval().
Type Casting
Casting converts one type to another. Use (int), (float), (string), (bool), or functions like intval(), floatval(), strval().
Real-life example: Casting is pouring juice from a big bottle into a small cup — same liquid, different container shape.
<?php
$price = "1999.50";
$intPrice = (int) $price; // 1999
$floatPrice = (float) $price; // 1999.5
$strCount = (string) 32; // "32"
echo $intPrice, " ", $floatPrice, " ", $strCount;
?>Math
PHP supports +, -, *, /, % (modulo), and ** (power). Use abs(), round(), ceil(), floor(), min(), max(), and rand() for common tasks.
Real-life example: Math in PHP is a calculator on the server — discount prices, split bills, or pick a random quiz question.
<?php
$a = 10;
$b = 3;
echo $a + $b; // 13
echo $a % $b; // 1 (remainder)
echo $a ** $b; // 1000 (power)
echo round(4.567, 2); // 4.57
echo rand(1, 10); // random 1–10
?>Constants
Constants hold values that never change. Define them with define('NAME', value) or const NAME = value inside classes.
Real-life example: A constant is the speed limit on a road — everyone reads the same number; it does not change mid-drive.
<?php
define("SITE_NAME", "Rishtaara");
define("MAX_LOGIN_ATTEMPTS", 5);
const COURSE_SLUG = "php-fundamentals";
echo SITE_NAME . " — " . COURSE_SLUG;
?>