Netbeans抛出语法错误 - php

时间:2012-02-17 01:03:46

标签: php mysql class

我只是好奇为什么Netbeans会在最后一行(花括号)的第三行抛出错误信息?谢谢你的帮助。

<?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;

}
} 
?>

3 个答案:

答案 0 :(得分: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;
    }

}

答案 1 :(得分: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;
    }
} 
?>

答案 2 :(得分: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"]));

推理: 您无法将get值直接拉入类中。您需要将变量传递给实例化的类。

$ example变量保存实例化的类。

相关问题