typestar

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

  1. intdiv divides toward zero while % keeps the remainder.
  2. (int) and (float) casts parse leading digits and drop the rest.
  3. Casting 19.99 to int truncates; round, floor, and ceil choose the direction.

Keywords and builtins used here

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.

Type this snippet

Step 3 of 3 in Variables & types, step 3 of 27 in Language basics.

← Previous Next →

Numbers & casts in other languages