Fatal error: 'continue' operator with non-constant operand is no longer supported

我的未来我决定 提交于 2020-01-06 10:53:40

问题


I'm playing with some code and getting this error, the script should work with PHP 5.2+, I'm running 5.4.27.

Complete error, Fatal error: 'continue' operator with non-constant operand is no longer supported in C:\xampp\htdocs\inc.php on line 326

Line 326, continue $scrape[$tracker] = self::set_error( new Exception( 'Tracker request timeout (' . $timeout . 's)' ), true );

The complete code section,

public function scrape ( $announce = null, $hash_info = null, $timeout = self::timeout ) {
    $packed_hash = urlencode( pack('H*', $hash_info ? $hash_info : $this->hash_info() ) );
    $handles = $scrape = array();
    if ( ! function_exists( 'curl_multi_init' ) )
        return self::set_error( new Exception( 'Install CURL with "curl_multi_init" enabled' ) );
    $curl = curl_multi_init();
    foreach ( (array) ($announce ? $announce : $this->announce()) as $tier )
        foreach ( (array) $tier as $tracker ) {
            $tracker = str_ireplace( array( 'udp://', '/announce', ':80/' ), array( 'http://', '/scrape', '/' ), $tracker );
            if ( isset( $handles[$tracker] ) )
                continue;
            $handles[$tracker] = curl_init( $tracker . '?info_hash=' . $packed_hash );
            curl_setopt( $handles[$tracker], CURLOPT_RETURNTRANSFER, true );
            curl_setopt( $handles[$tracker], CURLOPT_TIMEOUT, $timeout );
            curl_multi_add_handle( $curl, $handles[$tracker] );
        }
    do {
        while ( ( $state = curl_multi_exec( $curl, $running ) ) == CURLM_CALL_MULTI_PERFORM );
        if( $state != CURLM_OK )
            continue;
        while ( $done = curl_multi_info_read( $curl ) ) {
            $info = curl_getinfo( $done['handle'] );
            $tracker = array_shift( explode( '?', $info['url'], 2 ) );
            if ( empty( $info['http_code'] ) )
                continue $scrape[$tracker] = self::set_error( new Exception( 'Tracker request timeout (' . $timeout . 's)' ), true );
            elseif ( $info['http_code'] != 200 )
                continue $scrape[$tracker] = self::set_error( new Exception( 'Tracker request failed (' . $info['http_code'] . ' code)' ), true );
            $stats = self::decode_data( curl_multi_getcontent( $done['handle']  ) );
            curl_multi_remove_handle( $curl, $done['handle'] );
            $scrape[$tracker] = empty( $stats['files'] ) ?
                self::set_error( new Exception( 'Empty scrape data' ), true ) :
                array_shift( $stats['files'] ) + ( empty( $stats['flags'] ) ? array() : $stats['flags'] );
        }
    } while ( $running );
    curl_multi_close( $curl );
    return $scrape;
}

回答1:


continue is used to skip the rest of the current loop, and start with the next iteration. You can put a number behind it to tell how many loop levels out are to be skipped to the end of their iteration. If you don't specify a number, the defailt is 1.

But what you are putting behind continue not a number, but statements. Try to do the statements first, and then do continue;

So change:

if ( empty( $info['http_code'] ) )
    continue $scrape[$tracker] = self::set_error( new Exception( 'Tracker request timeout (' . $timeout . 's)' ), true );
elseif ( $info['http_code'] != 200 )
    continue $scrape[$tracker] = self::set_error( new Exception( 'Tracker request failed (' . $info['http_code'] . ' code)' ), true );

into:

if ( empty( $info['http_code'] ) ) {
    $scrape[$tracker] = self::set_error( new Exception( 'Tracker request timeout (' . $timeout . 's)' ), true );
    continue;
} elseif ( $info['http_code'] != 200 ) {
    $scrape[$tracker] = self::set_error( new Exception( 'Tracker request failed (' . $info['http_code'] . ' code)' ), true );
    continue;
}


来源:https://stackoverflow.com/questions/23805422/fatal-error-continue-operator-with-non-constant-operand-is-no-longer-supporte

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!