Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would this PHP be causing an execution timeout?

Tags:

php

wordpress

I've just released the first version of a WordPress plugin I wrote, and I've received a report that at least one of the people using my plugin is receiving an execution timeout error citing this block of code:

function getNumericAttributeFromHTML($htmlElement, $attribute){
    $attrStartPos = stripos($htmlElement, $attribute) + strlen($attribute);

    $strOffset = 0;
    $searchWithin = substr($htmlElement, $attrStartPos);

    while(!(is_numeric($searchWithin[$strOffset]))){
        $strOffset++;
    }
    $attrStartPos += $strOffset;

    $strOffset = 0;
    $searchWithin = substr($htmlElement, $attrStartPos);

    while((is_numeric($searchWithin[$strOffset]))){
        $strOffset++;
    }

    return substr($htmlElement, $attrStartPos, $strOffset);
}

This function is called twice per image on the page. Am I being crazy inefficient, or is it possible their host is just terrible?

Thanks in advance for any help you can provide.

like image 257
Greg Brown Avatar asked Dec 05 '25 09:12

Greg Brown


1 Answers

while(!(is_numeric($searchWithin[$strOffset]))) will run infinte times if $searchWithin has no numeric character!

Note the problem might be somewhere else.

To pinpoint the actual problem I suggest you get the reproducible steps from the bug and use a profiler to profile the code. You'll surely find where the problem is.

like image 189
Shiplu Mokaddim Avatar answered Dec 07 '25 21:12

Shiplu Mokaddim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!