2013-04-06 42 views
0

號分離功能我寫的獨立編號,並插入一個功能,每3個字符 想轉換123456789123,456,789錯誤在PHP

我的功能工作正常,但是當我用這個得到瀏覽器 我的功能3錯誤是這樣的:

function sefr($num){ 
    array($lnn); 
    $len=strlen($num); 
    $zc=intval($len/3); 
    for ($za=0;$za<$len;$za++){ 
    $lnn[$za]=substr($num,$za,1); 
    } 
    for ($zb=0;$zb<$len;$zb++){ 
     if ($zb==$len-(3*$zc)){ 
     if ($zb!=0){ 
      $lnnn=$lnnn.",$lnn[$zb]"; 
     } 
     else { 
      $lnnn=$lnnn.$lnn[$zb]; 
     } 
     $zc--; 
     } 
     else { 
      $lnnn=$lnnn.$lnn[$zb]; 
     } 
    } 
    return ($lnnn); 
} 

和我得到這個錯誤:

Notice: Undefined variable: lnn in file.php on line 2 
Notice: Undefined variable: lnnn in file.php on line 14 
Notice: Undefined variable: lnnn in file.php on line 19 
+3

使用[' number_format'](http://php.net/manual/en/function.number-format.php) – jeremy 2013-04-06 18:42:59

+0

這些都是* Notices *,這是由於未初始化已使用的變量而產生的。 – mario 2013-04-06 18:43:22

+0

謝謝, 我使用這個:$ number = number_format($ number); 是真的嗎? – Saitak 2013-04-06 18:47:01

回答

0

如果這是隻是例子,你是分組的字符串,那麼你可以使用chunk_split

echo trim(chunk_split($num, 3, ","),","); //123,456,78 

如果沒有,如果你正在處理的數字使用number_format

echo number_format($num, 0); //12,345,678 

請注意,123,456,78!= 12,345,678

+0

我使用這個:$ number = number_format($ number);這是真的嗎?它的工作正常 – Saitak 2013-04-06 19:10:00

+0

是的,它會正常工作...你應該先測試它 – Baba 2013-04-06 20:49:24