Numbers & casts in PHP
Integer and float arithmetic, plus the casts that tame loose strings.
<?php
$price = 19.99;
$qty = 3;
echo $price * $qty, PHP_EOL;
echo intdiv(17, 5), " remainder ", 17 % 5, PHP_EOL;
// casts are explicit and forgiving: leading digits parse, the rest drops
var_dump((int) "42 items");
var_dump((float) "3.14");
var_dump((int) $price);
echo round($price), " ", floor($price), " ", ceil($price), PHP_EOL;
How it works
intdivdivides toward zero while%keeps the remainder.(int)and(float)casts parse leading digits and drop the rest.- Casting
19.99to int truncates;round,floor, andceilchoose the direction.
Keywords and builtins used here
ceilechofloorintdivroundvar_dump
The run, in numbers
- Lines
- 12
- Characters to type
- 334
- Tokens
- 77
- Three-star pace
- 80 tpm
At the three-star pace of 80 tokens a minute, this run takes about 58 seconds.
Step 3 of 3 in Variables & types, step 3 of 27 in Language basics.