Asked  7 Months ago    Answers:  5   Viewed   42 times

Is there a way to test a range without doing this redundant code:

if ($int>$min && $int<$max)


Like a function:

function testRange($int,$min,$max){
    return ($min<$int && $int<$max);


if (testRange($int,$min,$max)) 


Does PHP have such built-in function? Or any other way to do it?



I don't think you'll get a better way than your function.

It is clean, easy to follow and understand, and returns the result of the condition (no return (...) ? true : false mess).

Wednesday, March 31, 2021
answered 7 Months ago

PHPSec does use composer, you should be able to just composer require "phpseclib/phpseclib=~0.3.10" and have it available (autoloaded).

$rsa = new Crypt_RSA();
Saturday, May 29, 2021
answered 5 Months ago

With ip2long() it's easy to convert your addresses to numbers. After this, you just have to check if the number is in range:

if ($ip <= $high_ip && $low_ip <= $ip) {
  echo "in range";
Thursday, June 3, 2021
answered 5 Months ago

If you are using Bash, you are better off using the arithmetic expression, ((...)) for readability and flexibility:

if ((number >= 2 && number <= 5)); then
  # your code

To read in a loop until a valid number is entered:


while :; do
  read -p "Enter a number between 2 and 5: " number
  [[ $number =~ ^[0-9]+$ ]] || { echo "Enter a valid number"; continue; }
  if ((number >= 2 && number <= 5)); then
    echo "valid number"
    echo "number out of range, try again"

((number >= 2 && number <= 5)) can also be written as ((2 <= number <= 5)).

See also:

  • Test whether string is a valid integer
  • How to use double or single brackets, parentheses, curly braces
Friday, July 30, 2021
answered 3 Months ago

Find the folder you installed PHPUnit in. There should be a file called phpunit.bat. It should have a line that reads something like

set PHPBIN="C:phpphp.exe"
%PHPBIN% "C:phpphpunit" %*

Change it to read

set PHPBIN="C:xamppphpphp.exe"
%PHPBIN% "C:xamppphpphpunit" %*

Or whatever the path to your PHP executable is

Friday, August 13, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :