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);
}

usage:

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

?

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

 Answers

20

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
 
AntoineB
answered 7 Months ago
91

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
 
ramdemon
answered 5 Months ago
23

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
 
ajaybc
answered 5 Months ago
29

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
fi

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

#!/bin/bash

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"
    break
  else
    echo "number out of range, try again"
  fi
done

((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
 
Shamoon
answered 3 Months ago
27

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
 
Migwell
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 :