PHP Training → Lesson 02
Strings, integers, floats, booleans, and null — and how PHP handles types dynamically.
$ prefixnull type and when to use itgettype() and var_dump()<?php
// String
$name = "Mr. Script";
$greeting = 'Hello, World!';
// Integer
$age = 42;
$year = 2025;
// Float
$price = 9.99;
$pi = 3.14159;
// Boolean
$isAvailable = true;
$isLoggedIn = false;
// Null
$nothing = null;
// Check the type
echo gettype($name); // string
echo gettype($age); // integer
echo gettype($price); // double (PHP calls floats "double")
echo gettype($isAvailable); // boolean
echo gettype($nothing); // NULL
// Inspect value AND type at once
var_dump($name);
var_dump($isAvailable);
?>
<?php
$strNum = "42"; // This is a string
$num = (int) $strNum; // Cast to integer: 42
$floatNum = 3.99;
$intNum = (int) $floatNum; // Truncates to 3 (not rounded)
$boolTrue = (bool) 1; // true
$boolFalse = (bool) 0; // false
$boolStr = (bool) ""; // false — empty string is falsy
// PHP will also juggle types automatically
$result = "5" + 3; // 8 — PHP converts "5" to integer
echo $result; // 8
echo gettype($result); // integer
?>
<?php
$first = "Eric";
$last = "Script";
// Concatenation with dot operator
$full = $first . " " . $last;
echo $full; // Eric Script
// String interpolation (double quotes only)
echo "Hello, $first!"; // Hello, Eric!
echo "Hello, {$first}!"; // Same — curly braces for clarity
// Single quotes: no interpolation
echo 'Hello, $first!'; // Hello, $first! (literal)
// Useful string functions
echo strlen($full); // 11
echo strtoupper($full); // ERIC SCRIPT
echo strtolower($full); // eric script
echo str_replace("Eric", "Mr.", $full); // Mr. Script
?>
Use var_dump() for debugging. It shows the type and value together — much more useful than echo when you're not sure what you're dealing with.
Watch out for type juggling. PHP's automatic type conversion is convenient but can cause subtle bugs. If you need strict comparisons, use === instead of ==.