Jump to content

ekowk

New Members
  • Posts

    1
  • Joined

  • Last visited

Everything posted by ekowk

  1. Dear All PHP Master, Kindly need your help (I'm sorry my Bad English), I'm new in PHP and OOP in PHP please help to check I have class to convert number to words below, but when I'm running it the result in browser is error with error message : Notice: Undefined variable: str in C:\xampp\htdocs\latihan\oop.php on line 16 Notice: Undefined variable: str in C:\xampp\htdocs\latihan\oop.php on line 14 Notice: Undefined variable: str in C:\xampp\htdocs\latihan\oop.php on line 16 seratus dua puluh tiga juta empat ratus lima puluh enam ribu tujuh ratus delapan puluh sembilan here it's my code to convert number to words : <?php Class Terbilang { Public function terbilang() { $this->dasar = array(1=>'satu','dua','tiga','empat','lima','enam','tujuh','delapan','sembilan'); $this->angka = array(1000000000000,1000000000,1000000,1000,100,10,1); $this->satuan = array('triliun','milyar','juta','ribu','ratus','puluh',''); } Public function eja($n) { $i=0; while($n!=0){ $count = (int)($n/$this->angka[$i]); if($count>=10) $str .= $this->eja($count). " ".$this->satuan[$i]." "; else if($count > 0 && $count < 10) $str .= $this->dasar[$count] . " ".$this->satuan[$i]." "; $n -= $this->angka[$i] * $count; $i++; } $str = preg_replace("/satu puluh (\w+)/i","\\1 belas",$str); $str = preg_replace("/satu (ribu|ratus|puluh|belas)/i","se\\1",$str); return $str; } } $bilangan = new Terbilang; echo $bilangan -> eja(123456789); //shouldbe show :seratus dua puluh tiga juta empat ratus lima puluh enam ribu tujuh ratus delapan puluh sembilan ?>
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.