PHP Ayrıştırma Hatası beklenmedik '{' [kapalı]

4 Cevap php

Ben bir ": sözdizimi hatası, beklenmedik '{' hattında 2 Ayrıştırma hatası" alıyorum. Ve ben sorun görmüyorum.

<?php
class pointLocation {
    var $pointOnVertex = true; // Check if the point sits exactly on one of the vertices

    function pointLocation() {
    }
    
    
        function pointInPolygon($point, $polygon, $pointOnVertex = true) {
        $this->pointOnVertex = $pointOnVertex;
        
        // Transform string coordinates into arrays with x and y values
        $point = $this->pointStringToCoordinates($point);
        $vertices = array(); 
        foreach ($polygon as $vertex) {
            $vertices[] = $this->pointStringToCoordinates($vertex); 
        }
        
        // Check if the point sits exactly on a vertex
        if ($this->pointOnVertex == true and $this->pointOnVertex($point, $vertices) == true) {
            return "vertex";
        }
        
        // Check if the point is inside the polygon or on the boundary
        $intersections = 0; 
        $vertices_count = count($vertices);
    
        for ($i=1; $i < $vertices_count; $i++) {
            $vertex1 = $vertices[$i-1]; 
            $vertex2 = $vertices[$i];
            if ($vertex1['y'] == $vertex2['y'] and $vertex1['y'] == $point['y'] and $point['x'] > min($vertex1['x'], $vertex2['x']) and $point['x'] < max($vertex1['x'], $vertex2['x'])) { // Check if point is on an horizontal polygon boundary
                return "boundary";
            }
            if ($point['y'] > min($vertex1['y'], $vertex2['y']) and $point['y'] <= max($vertex1['y'], $vertex2['y']) and $point['x'] <= max($vertex1['x'], $vertex2['x']) and $vertex1['y'] != $vertex2['y']) { 
                $xinters = ($point['y'] - $vertex1['y']) * ($vertex2['x'] - $vertex1['x']) / ($vertex2['y'] - $vertex1['y']) + $vertex1['x']; 
                if ($xinters == $point['x']) { // Check if point is on the polygon boundary (other than horizontal)
                    return "boundary";
                }
                if ($vertex1['x'] == $vertex2['x'] || $point['x'] <= $xinters) {
                    $intersections++; 
                }
            } 
        } 
        // If the number of edges we passed through is even, then it's in the polygon. 
        if ($intersections % 2 != 0) {
            return "inside";
        } else {
            return "outside";
        }
    }

    
    
    function pointOnVertex($point, $vertices) {
        foreach($vertices as $vertex) {
            if ($point == $vertex) {
                return true;
            }
        }
    
    }
        
    
    function pointStringToCoordinates($pointString) {
        $coordinates = explode(" ", $pointString);
        return array("x" => $coordinates[0], "y" => $coordinates[1]);
    }
    
    
}



$pointLocation = new pointLocation();
$points = array("30 19", "0 0", "10 0", "30 20", "11 0", "0 11", "0 10", "30 22", "20 20");
$polygon = array("10 0", "20 0", "30 10", "30 20", "20 30", "10 30", "0 20", "0 10", "10 0");
foreach($points as $key => $point) {
    echo "$key ($point) is " . $pointLocation->pointInPolygon($point, $polygon) . "<br>";
}
?>

Herkes sorunu görüyor mu?

Teşekkürler,

-Laxmidi

4 Cevap

Benim son ince çalışır, bu yüzden bir kodlama veya EOL sorunu gibi geliyor. Bunları deneyin:

  • Dosya UNIX EOL biçiminde olduğundan emin olun. Her satır bir satır besleme (LF) ile bitmelidir
  • Eğer dosyaları bir Reçetesi (bayt sırası işareti) yok emin olun.
  • Size dosyanın sonundaki ?> kurtulmak deneyebilirsiniz. Hiçbir amaca hizmet eder. Verilen bu gerçekten sorunu çözmek için yardımcı olmaz, ancak sizin kod biraz temiz yapacaktır.
  • Lütfen açılış <?php sonra ve class pointLocation { sonra bir satır sonu eklemeyi deneyin. Garip boşlukla PHP gezisi kadar bazı sürümleri.
  • Dosya UTF-8 gibi bir şey standart olduğundan emin olun

Dosya bu kriterleri karşıladığından emin olmak için iyi bir editör Notepad++. Orada birçok seçenek vardır ve onlar ölü kolaylaştırır. Microsoft CRLF formatında (değil PHP-dostu) içine kilitler ve kodlama üzerinde kontrol vermez çünkü, düz eski Not Defteri'ni kullanın etmeyin.

İyi şanslar!

Test. Hatasız çalışır ve outout aşağıdaki üretir:

0 (30 19) is boundary<br>1 (0 0) is outside<br>2 (10 0) is vertex<br>3 (30 20) is vertex<br>4 (11 0) is boundary<br>5 (0 11) is boundary<br>6 (0 10) is vertex<br>7 (30 22) is outside<br>8 (20 20) is inside<br>

(Mac OS x 10.6.3, PHP 5.3.1 cli)

EDIT: hata include / gerektiren aracılığıyla sınıf tanımını çağırır senaryonun bazı diğer kısmı yatıyor Belki orada neden olur.

Daha detaylı bilgi yardımcı olabilir.

Techpriester gibi test, hatasız çalışır ve aşağıdaki çıktıyı üretir:

0 (30 19) is boundary<br>1 (0 0) is outside<br>2 (10 0) is vertex<br>3 (30 20) is vertex<br>4 (11 0) is boundary<br>5 (0 11) is boundary<br>6 (0 10) is vertex<br>7 (30 22) is outside<br>8 (20 20) is inside<br>

Ubuntu 9.10, Linux 2.6.31-17 üzerinde test edilmiştir, PHP 5.3.2-0.dotdeb.2 (cli)

Bu hata olabilir herhangi bir auto-prepended files (auto_prepend_file) için php.ini kontrol etmeyi deneyin.

Kod herkes için çalıştı, ben kopyalama ve geri benim sisteme Stackoverflow kodu yapıştırarak denedim ve çalıştı. Tuhaf. Ben biçimlendirme ile berbat bir şey olmalıydı.

Ben hala sorunun ne olduğunu bilmiyorum, ama en azından çalışıyor.

Mattbasta, Techpriester ve Zack teşekkür ederiz.