PHP: Check whether an integer is in a certain interval or not PHP: Check whether an integer is in a certain interval or not symfony symfony

PHP: Check whether an integer is in a certain interval or not


Since PHP 5.2 there are filter_var() functions with considerable set of options available.

One of them lets you check for number being in a range:

$param = 10;$result = filter_var( $param, FILTER_VALIDATE_INT, [    'options' => [        'min_range' => 20,         'max_range' => 40    ]]);var_dump( $result ); // will return FALSE for 10

http://codepad.viper-7.com/kVwx7L


You should consider adding some constraints on your entity from the validation component of Symfony2.

use Symfony\Component\Validator\Constraints as Assert;class Achievement{   /**    * @Assert\Range(min=0, max=100)    */    protected progress;}

The validator service is called automatically when for example validating forms, but you can even call it manually by getting the validator service, for example in your controller.

$achievement = new Achievement();$errors = $this->get('validator')->validate($achievement);


You can use the Range constraint:

use Symfony\Component\Validator\Constraints\Range;class Achievement{    /**     * @Range(min=0, max=100)     */    private $progress;}

Validation in Symfony is handled by a separate layer, so you should not do it in setters.