Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6984085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:30:16+00:00 2026-05-27T18:30:16+00:00

I have a problem with my recursive function in PHP. The aim of my

  • 0

I have a problem with my recursive function in PHP. The aim of my function is to compare an IP address with a range of IP addresses. Here is my data model for the two variables:

Array
(
   [0] => 150
   [1] => 2
   [2] => 0
   [3] => 155
)
Array
(
   [0] => Array
       (
           [0] => 150
           [1] => 26
           [2] => 0
           [3] => 0
       )

   [1] => Array
       (
           [0] => 150
           [1] => 100
           [2] => 255
           [3] => 255
       )
)

I know the iterative method will be probably better, but it’s just for training with recursive methods.

So here is my code:

function checkRangeIp($ip_array, $range_ip_array, $recur_it = 0) {
  if ($recur_it == 4)
    return false;

  $nb1 = $ip_array[$recur_it];
  $nb2 = $range_ip_array[0][$recur_it];
  $nb3 = $range_ip_array[1][$recur_it];

  if ($nb1 < $nb2 || $nb1 > $nb3)
    return true;
  else
    checkRangeIp($ip_array, $range_ip_array, $recur_it + 1);
}

I don’t know why, but when I test my function it always giving me a false return.

if (checkRangeIp($ip_array, $range_ip_array))
  echo "TRUE\n";
else
  echo "FALSE\n";

How can I fix this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:30:16+00:00Added an answer on May 27, 2026 at 6:30 pm

    You have an exit path of checkRangeIp which does not return any value (namely the recursive call). Replace it by

    return checkRangeIp($ip_array, $range_ip_array, $recur_it + 1);
    

    otherwise the function call will be evaluated as false inside your if(...) condition.

    Note: It looks like you mixed up somehow true and false. The condition ($nb1 < $nb2 || $nb1 > $nb3) is true if the value lies outside of the range and in this case the function returns true.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with a recursive SQL function. The original problem is that
I'm in PHP working on an Euler problem. I have this function so far:
I have problem when I try insert some data to Informix TEXT column via
Recursive function for copy of multilevel folder is not working. I have a code
I have a recursive function being used to generate a menu on my site.
I have a javascript function that walks a tree recursively. It has two flag
I have a recursive function to iterate over 11M database records, 1000 at a
I have a strongly recursive function, that creates a (very small) std::multimap locally for
I have written a simple depth-first search in Scala with a recursive function like
I have a recursive function which contains some drawing code inside. I was advised

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.