November 25, 2026 - 09:44
PHP Lesson 19 — Is Casting to an Integer the Same as Validating One? Image
PHP Tutorials

PHP Lesson 19 — Is Casting to an Integer the Same as Validating One?

0 Comments

A field expects a number. We cast the input with (int), and now the variable is an integer. But did the user submit a valid number in the first place? Converting a value and validating an input rule are different operations.

Use a quantity field with an explicit rule: accept integers from 0 through 100. Zero is valid; negative numbers and nonnumeric text are not. Stating the rule first gives us something concrete to test.

See what a cast loses

Create experiment.php in a new folder and run it there with php experiment.php.

PHP
<?php
foreach (['12', '12cats', 'apple', '0', '-3'] as $raw) {
    echo $raw, ' => ', (int) $raw, PHP_EOL;
}

12cats becomes 12, and apple becomes 0. Looking only at those converted numbers no longer tells us what was submitted. In particular, apple and 0 produce the same integer, although our rule should reject one and accept the other.

(int) converts a type; it does not approve the meaning of the input. Converting a previously validated numeric value is different from silently treating invalid user input as an acceptable quantity.

Apply a validation rule to the same inputs

PHP
<?php
foreach (['12', '12cats', 'apple', '0', '-3'] as $raw) {
    echo $raw, ' => ';
    var_dump(filter_var($raw, FILTER_VALIDATE_INT));
}

Now 12cats and apple produce bool(false). The strings 12, 0, and -3 return integer values. FILTER_VALIDATE_INT asks whether they can be validated as integers. We have not yet specified nonnegative values or an upper limit of 100.

The first argument is the value to inspect; the second is the filter to apply. On success, we get a usable integer rather than just a boolean. That is why a successful zero must be distinguished from the failure value false.

Do not reject a successful zero

PHP
<?php
$count = filter_var('0', FILTER_VALIDATE_INT);

if (!$count) {
    echo "Wrong check rejects zero", PHP_EOL;
}

if ($count === false) {
    echo "Invalid integer", PHP_EOL;
} else {
    echo "Valid integer: ", $count, PHP_EOL;
}

The first check prints Wrong check rejects zero. It evaluates zero as false-like. The second check asks for the exact boolean false and correctly prints Valid integer: 0. The distinction from the comparison lesson now matters in a real input check.

Add the 0–100 range

PHP
<?php
$raw = $_GET['count'] ?? '0';

if (!is_string($raw)) {
    echo "Invalid input type";
    return;
}

$count = filter_var($raw, FILTER_VALIDATE_INT, [
    'options' => ['min_range' => 0, 'max_range' => 100],
]);

if ($count === false) {
    echo "Enter an integer from 0 to 100";
    return;
}

echo "Accepted: ", $count;

The complete example checks is_string() because we expect an ordinary text input. The third argument supplies an options array with min_range and max_range. Now an integer must also fall inside the permitted range.

Start php -S 127.0.0.1:8001 in the folder and open http://127.0.0.1:8001/experiment.php?count=0. Expect Accepted: 0. count=100 also passes. Try count=-1, count=101, count=12cats, and count=1.5; they fail the integer/range rule. count[]=12 supplies an array and is rejected by the input-type check.

This is a quantity rule. A product code, phone number, or identifier whose leading zeros matter needs a different decision. If you must preserve 0012, you may need a string with a defined format rather than an integer conversion.

Calling filter_var alone does not specify validation

PHP
<?php
$raw = '<b>12</b>';

var_dump(filter_var($raw));
var_dump(filter_var($raw, FILTER_VALIDATE_INT));

The first call returns string(9) "<b>12</b>". It neither removes the markup nor validates an integer. With no explicit filter, the default behavior passes this value through. The second call states the rule and returns bool(false).

Finish by switching the browser between count=0, count=100, and count=101. Seeing both accepted boundaries and the first rejected value makes it clear which application rule the code enforces.

Where this appeared in the task list: PHP Lesson 07 — Change a Task's Completion Status

Official documentation: filter_var · Filter constants · Integer conversion

Related Articles

Comments (0)

No comments yet. Be the first to comment!

Leave a Comment