Variables and types

Variables start with $; int, float, string, bool, null, arrays; strict comparison and conversions.

Runs in Zerethon IDE · PHP 8.4 Official PHP documentation ↗
main.php
<?php

$count = 3;
$price = 9.5;
$name = 'Ada';
$ready = true;
$nothing = null;

echo gettype($count), ' ', gettype($price), ' ', gettype($name), ' ', gettype($ready), ' ', gettype($nothing), "\n";
echo "$name costs {$price}\n";
echo 7 / 2, ' ', intdiv(7, 2), ' ', 7 % 2, ' ', 2 ** 10, "\n";
echo (int) '42' + 1, ' ', (float) '2.5' * 2, ' ', (string) 7 . '7', "\n";
var_dump('1' == 1, '1' === 1, 0 == 'a');

Output

integer double string boolean NULL
Ada costs 9.5
3.5 3 1 1024
43 5 77
bool(true)
bool(false)
bool(false)
  • Use === / !== (type and value). == converts types first.
  • / gives a float when the result is not whole; intdiv() is whole-number division.
  • var_dump() shows type and value — the quickest way to inspect something.
  • Add declare(strict_types=1); at the top of a file to stop scalar type conversion in function calls.