php

Validate a number using filter_var() method in PHP

If you want to validate a number in PHP you can use the filter_var() method and pass value and FILTER_VALIDATE_INT as parameters.

<?php
    $value = 500;

    if (!filter_var($value, FILTER_VALIDATE_INT) === false) {
        echo("Value is valid");
    } else {
        echo("Value is invalid");
    }

    // -> Value is valid
?>
Was this helpful?