$ x * $ y = $ z为零

时间:2013-02-23 08:29:48

标签: php math

我必须从RON tu改变价格。我在操作中得到0

我用过的代码:

  include('./panou/simple_html_dom.php');
  function euro() {
       $dom= file_get_html("http://www.cursvalutar.ro/");;  
       foreach ($dom->find('tr') as $node) {
       if (is_a($node->children(1), 'simple_html_dom_node')) {
            if ($node->children(1)->plaintext == "Euro") {
            $plain = explode(',', $node->children(2)->plaintext);           
         if(!isset($plain[0])!== false) {
                      echo("Nu are");
                  }
          elseif(stripos($plain[0], $plain[0])!== false{
                      "4.30"
                  }
             }
        }
   }
   $plain[0]*=$koyos; echo "$plain[0]";}

$plain[0] = 4.3780$koyos = 545.66脚本的结果为0

2 个答案:

答案 0 :(得分:1)

相反,请使用此网址获取RON-EUR的实际价值:http://download.finance.yahoo.com/d/quotes.csv?s=RONEUR=X&f=sl1d1t1ba&e=.csv 解析CSV文件比HTML文件更容易。

答案 1 :(得分:0)

代码问题: $ koyos变量未在euro()函数中定义。 将它传递给函数。 如果要打印数组的元素,您应该这样做:

     echo $plain[0]
     echo "{$plain[0]}"    //  this two prints what you want 

这不是好方法:

     echo "$plain[0]"      // the result of this might be "Array[0]" 

但最好用这个代替你的代码:

    function getEuroRon(){
            if (!$file = fopen('http://download.finance.yahoo.com/d/quotes.csv?s=EURRON=X&f=sl1d1t1ba&e=.csv','r')){
                    die('resource can not be read');
            }
            $data = fgetcsv($file);
            fclose($file);
            return $data[1];
    }
    $euroron = getEuroRon();
    /*
    //... somewhere in code:
    $koyos = 545.66;

    //i dont recommend to use global variables but you might need this:
    function euro(){
          global $koyos,$euroron;
          print $euroron * $koyos;
    }
    */
    //This should be better instead the previous:
    function euro($koyos){
          global $euroron;
          print $euroron * $koyos;
    }   
    euro(545.66);

您可以在需要打印结果的地方调用euro()函数... 应该有更好的方式,但我不知道你的代码的其他细节,并尝试解决你的问题几乎与你编码的方式。

或者您可以使用更好的解决方案: http://pastebin.com/HMp8SR2j