Wikis & learning

Store a value and inspect its type

A value arriving from an HTML form usually begins life as a string. The shape of its printed output can hide that distinction.

A value arriving from an HTML form usually begins life as a string. The shape of its printed output can hide that distinction.

How it works

Compare the values with strict equality, then use an explicit conversion when the input has already been validated.

PHP example

<?php
$fromForm = '12';
$quantity = 12;
var_dump($fromForm === $quantity);
var_dump((int) $fromForm === $quantity);

Expected result

bool(false) followed by bool(true)

Try a variation

Replace the string with 012. Observe how the original text changes while the integer conversion still represents twelve.