2012-02-17 99 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; 
    } 
} 
?>