1

I have part of a PHP script that's behaving odd for me. The below loop grabs the highest value from the builds array - which is a bunch of integers in a random order. It grabs the highest value as expected, no problems there.

$highest = $builds[0];
    for ($i=0; $i < count($builds); $i++) {
        if ($builds[$i] > $highest) {
            $highest = $builds[$i];
    }
}

The problem is when I try to use $highest after the loop. (file_get_html is part of the simple_html_dom library - I use it fine in other parts of the script - it's not an issue.)

$html = file_get_html("http://www.blah.com/builds/" . $highest);
//timeout msg on browser

This ends up timing out. Chrome comes back with "Error 101: The connection was reset". Firefox comes back with a similar, "The connection to the server was reset while the page was loading."

I've played around a bit, and best I can tell is that $highest isn't being set or called correctly somehow.

If I assign the variable manually, file_get_html works fine and returns data as expected. I.E. If I make my script looks like this.

$highest = $builds[0];
    for ($i=0; $i < count($builds); $i++) {
        if ($builds[$i] > $highest) {
            $highest = $builds[$i];
    }
}

$highest = 20;  //I understand the loop is useless because of this
                //but I've been running it to make sure it's not the issue.

$html = file_get_html("http://www.blah.com/builds/" . $highest);
//now this returns data

Why does manually assigning the variable work, but setting it in the loop doesnt? I've put in prints and var_dumps along the way to make sure the variable is being set correctly, and it appears to be. Mind boggled.

3
  • 1
    whats the output of var_dump($highest) after loop? Commented Feb 4, 2013 at 23:59
  • try an $highest = intval($highest); after the loop. Commented Feb 5, 2013 at 0:02
  • 1
    @Bernhard - the result is string '20' (length=2) @Chris, no dice. Forgot to mention I also tried doing (string) to no avail. Commented Feb 5, 2013 at 0:19

2 Answers 2

1

Since the php max() function can take an array, a simpler version is

$highest=max($builds);
$html = file_get_html("http://www.blah.com/builds/" . $highest);
Sign up to request clarification or add additional context in comments.

Comments

0

Giving this a test, all seems to work.

$builds = array(10, 5, 6, 9, 22, 54, 33, 72, 9);
$highest = $builds[0];
for ($i=0; $i < count($builds); $i++) {
    if ($builds[$i] > $highest) {
    $highest = $builds[$i];
    }
}

echo $highest;

Echoing $highest outputs 72, which is the highest number in the array. What type of array is $builds, what is the var_dump of the $builds array?

1 Comment

$builds var_dump = array (size=18) 0 => string '4' (length=1) 1 => string '16' (length=2) 2 => string '8' (length=1) 3 => string '2' (length=1) 4 => string '14' (length=2) 5 => string '12' (length=2) 6 => string '11' (length=2) 7 => string '10' (length=2) 8 => string '9' (length=1) 9 => string '1' (length=1) 10 => string '7' (length=1) 11 => string '17' (length=2) 12 => string '13' (length=2) 13 => string '5' (length=1) 14 => string '3' (length=1) 15 => string '15' (length=2) 16 => string '1' (length=1) 17 => string '0' (length=1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.