php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #38453 PHP could use a number_between() function
Submitted: 2006-08-14 15:29 UTC Modified: 2006-08-14 15:33 UTC
From: z_rules55 at hotmail dot com Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 5.1.4 OS:
Private report: No CVE-ID: None
 [2006-08-14 15:29 UTC] z_rules55 at hotmail dot com
Description:
------------
I think PHP could use a function to determine whether a given number is between a specified lower and upper bound, with the option to specify whether those bounds should be included. It would save people a bit of work in having to code their own function or repeatedly type "if(x < upperBound && x > lowerBound)". A (very, very quick) search through the PEAR Math packages didn't turn up what I'm thinking of. Something like the following code:

Reproduce code:
---------------
<?php
// Returns whether or not an operand is between two
// values, and can optionally do the comparison using
// upper and lower bounds inclusive.
function between($operand, $lower_bound, $upper_bound, $inclusive = true) {
    if($inclusive) {
        return ($operand >= $lower_bound && $operand <= $upper_bound);
    }
    else {
        return ($operand > $lower_bound && $operand < $upper_bound);
    }
}
?>


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-08-14 15:33 UTC] tony2001@php.net
There is absolutely no need for a function that can be implemented in 3 lines of PHP code.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 16:01:29 2024 UTC