Variables start with $ and are loosely typed by default—PHP figures out the type from the value. PHP 7+ adds scalar type hints and PHP 8+ strengthens the type system with union types and more.
Scalar types
string— text in single or double quotesint/float— numbers; watch division (10 / 4is float2.5)bool—true/false
Assignment and reassignment
$count = 0;
$count = $count + 1;
$name = 'Tuto';
Unset with unset($name) when you need to remove a variable from the current scope.
Constants
define('APP_NAME', 'Tuto'); or const APP_VERSION = '1.0'; at class/top level. Constants are not prefixed with $.
Important interview questions and answers
- Q: Is PHP statically typed?
A: Historically no; you can enable strict types per file withdeclare(strict_types=1);and use type hints for parameters and return values. - Q:
vs===?
A:==compares with type juggling;===compares value and type—prefer===unless you explicitly need coercion.
Self-check
- What prefix do all variables use?
- Name three scalar types.
Pitfall: Use === for comparisons—loose == coerces types and causes subtle bugs (0 == 'foo').
Interview prep
- == vs === in PHP?
===compares value and type;==coerces types and causes legacy surprises—prefer strict comparison in application code.