0

i have a script where i use die to prevent a function's continuous loop. But if i place this above the html, the html script will stop as well so i place it below the html but all the variables get echoed below the actual website. How can i make sure this get's echoed to the place where i want it to be and not below the website? Or is there a different way than using die? This is the code of the function:

function QueryWhoisServer($whoisserver, $domain) {
    $port = 43;
    $timeout = 5;
    $errm = "<p1>No connection could be made</p1>";
    $fp = @fsockopen($whoisserver, $port, $errno, $errstr, $timeout) or die($errm); 
    fputs($fp, $domain . "\r\n");
    $out = "";
    while (!feof($fp)) {
        $out .= fgets($fp);
    }
    fclose($fp);

    $res = "";
    if((strpos(strtolower($out), "error") === FALSE) && (strpos(strtolower($out), "not allocated") === FALSE)) {
        $rows = explode("\n", $out);
        foreach($rows as $row) {
            $row = trim($row);
            if(($row != ':') && ($row != '#') && ($row != '%')) {
                $res .= $row."<br>";
            }
        }
    }
    return $res;
}
1
  • Where you want to come out from function ? Commented Nov 20, 2014 at 11:53

3 Answers 3

2

The keyword break breaks out of any loop, just use it instead of die.

Beware if you have nested loops, as break will only exit the innermost loop. Oddly enough, in php you can use break(2) to break from two loops. I would refrain from doing that though.

Sign up to request clarification or add additional context in comments.

Comments

1

die(); stops all PHP execution. It's rare that you'd actually want to do that.

Instead you should look at the try - catch construct and throwing and catching exceptions.

function QueryWhoisServer($whoisserver, $domain) {
    try {
        $port = 43;
        $timeout = 5;
        $errm = "<p1>No connection could be made</p1>";
        $fp = @fsockopen($whoisserver, $port, $errno, $errstr, $timeout);

        if (!fp) {
            throw new Exception ("Couldn't open socket.");
        }
        //after the exception is thrown, the rest of this block will not execute.

        fputs($fp, $domain . "\r\n");
        $out = "";
        while (!feof($fp)) {
            $out .= fgets($fp);
        }
        fclose($fp);

        $res = "";
        if((strpos(strtolower($out), "error") === FALSE) && (strpos(strtolower($out), "not allocated") === FALSE)) {
            $rows = explode("\n", $out);
            foreach($rows as $row) {
                $row = trim($row);
                if(($row != ':') && ($row != '#') && ($row != '%')) {
                    $res .= $row."<br>";
                }
            }
        }
        return $res;
    } catch (Exception $e) {
        //this block will be executed if any Exception is caught, including the one we threw above.
        //you can handle the error here or rethrow it to pass it up the execution stack.
        return "";
    }

}

PHP's Exceptions manual page

Comments

0

You can use a control variable to avoid infinite looping.

$end_condition = false;
while (!$end_condition)
{
    //do the job
    if (conditions/are/met)
    {
        $end_condition = true;
    }
}

Comments

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.