2012-02-17 96 views
1

我只是好奇,爲什麼Netbeans會在最後一行的第三行中引發錯誤消息,大括號?謝謝您的幫助。Netbeans引發語法錯誤-php

<?php 

    class convert{ 

var $amnt = htmlspecialchars($_GET["amnt"]); 
var $cc_from = htmlspecialchars($_GET["from"]); 
var $cc_to = htmlspecialchars($_GET["to"]); 

function convert($amnt,$cc_from,$cc_to,$decimals=2){ 
$db_rate_from = mysql_query("SELECT * FROM $db_tableprefix WHERE country_code='$cc_from'"); 
$query_row_from = mysql_fetch_array($db_rate_from); 
$rate_from = ($query_row_from[rate]); 
echo $rate_from; 
echo "</br>rate to</br>"; 

$db_rate_to = mysql_query("SELECT * FROM $db_tableprefix WHERE country_code='$cc_to'"); 
$query_row_to = mysql_fetch_array($db_rate_to); 
$rate_to = ($query_row_to[rate]); 
echo $rate_to; 
echo "</br>conversion</>"; 

var $conversion = (number_format(($amnt/$rate_from)*$rate_to,$decimals)); 
echo $conversion; 

} 
} 
?> 
+1

所選答案不能解決代碼的所有問題。函數中的'var'也會導致錯誤(請參閱我的無錯代碼答案)。 – 0b10011 2012-02-17 01:31:36

回答

1

您不能在類中聲明類似的變量。你想要更像這樣的東西:

class convert 
{ 

    public $amnt; // don't worry about what public means 
    public $cc_from; // if you want to know, have a look at 
    public $cc_to; // http://php.net/public 

    // This runs when the class is created with `new convert()` 
    public function __construct() 
    { 
     $this->amnt = htmlspecialchars($_GET["amnt"]); // sets that $amnt up there 
     $this->cc_from = htmlspecialchars($_GET["from"]); 
     $this->cc_to = htmlspecialchars($_GET["to"]); 
    } 

    // All that's changed here is making $amnt into $this->amnt, etc 
    function convert($amnt,$cc_from,$cc_to,$decimals=2) 
    { 
     $db_rate_from = mysql_query("SELECT * FROM $db_tableprefix WHERE country_code='$this->cc_from'"); 
     $query_row_from = mysql_fetch_array($db_rate_from); 
     $rate_from = ($query_row_from[rate]); 
     echo $rate_from; 
     echo "</br>rate to</br>"; 

     $db_rate_to = mysql_query("SELECT * FROM $db_tableprefix WHERE country_code='$cc_tothis->'"); 
     $query_row_to = mysql_fetch_array($db_rate_to); 
     $rate_to = ($query_row_to[rate]); 
     echo $rate_to; 
     echo "</br>conversion</>"; 

     $conversion = (number_format(($this->amnt/$rate_from) * $rate_to, $decimals)); 
     echo $conversion; 
    } 

} 
+0

這是不正確的,因爲函數中的'var'也會引發錯誤。 – 0b10011 2012-02-17 01:31:01

+0

錯過了那個,很好。固定。 – Joe 2012-02-17 01:33:13

0

你在代碼中有一些錯誤。嘗試修改此。

<?php 

    class convert { 

     var $amnt; 
     var $cc_from; 
     var $cc_to; 

     function convert($amnt,$cc_from,$cc_to,$decimals=2){ 

      $this->amnt = $amnt; 
      $this->cc_from = $cc_from; 
      $this->cc_to = $cc_to; 

      $db_rate_from = mysql_query("SELECT * FROM $db_tableprefix WHERE country_code='$cc_from'"); 
      $query_row_from = mysql_fetch_array($db_rate_from); 
      $rate_from = ($query_row_from[rate]); 
      echo $rate_from; 
      echo "</br>rate to</br>"; 

      $db_rate_to = mysql_query("SELECT * FROM $db_tableprefix WHERE country_code='$cc_to'"); 
      $query_row_to = mysql_fetch_array($db_rate_to); 
      $rate_to = ($query_row_to[rate]); 
      echo $rate_to; 
      echo "</br>conversion</>"; 

      $conversion = (number_format(($amnt/$rate_from)*$rate_to,$decimals)); 
      echo $conversion; 
     } 
    } 

    $example = new convert(htmlspecialchars($_GET["amnt"]), htmlspecialchars($_GET["from"]), htmlspecialchars($_GET["to"])); 

推理: 您無法將值直接拉入類中。您需要將變量傳遞給實例化的類。

$ example變量包含實例化的類。

1

類變量必須是常量。他們可以設置爲__construct()。在一個函數中不能使用var。我已經評論了下面的所有更改。

<?php 
class convert { 
    var $amnt = ""; // Set to "" 
    var $cc_from = ""; // Set to "" 
    var $cc_to = ""; // Set to "" 
    // Added __construct() to set defaults when initialized 
    function __construct(){ 
     $this->amnt = htmlspecialchars($_GET["amnt"]); 
     $this->cc_from = htmlspecialchars($_GET["from"]); 
     $this->cc_to = htmlspecialchars($_GET["to"]); 
    } 
    function convert($amnt, $cc_from, $cc_to, $decimals=2){ 
     $db_rate_from = mysql_query("SELECT * FROM $db_tableprefix WHERE country_code='$cc_from'"); 
     $query_row_from = mysql_fetch_array($db_rate_from); 
     $rate_from = ($query_row_from[rate]); 
     echo $rate_from; 
     echo "</br>rate to</br>"; 
     $db_rate_to = mysql_query("SELECT * FROM $db_tableprefix WHERE country_code='$cc_to'"); 
     $query_row_to = mysql_fetch_array($db_rate_to); 
     $rate_to = ($query_row_to[rate]); 
     echo $rate_to; 
     echo "</br>conversion</>"; 
     $conversion = (number_format(($amnt/$rate_from)*$rate_to,$decimals)); // Removed 'var' 
     echo $conversion; 
    } 
} 
?>