PHPで引数が重複するケースの処理についてお尋ねします。
###実現したい事
後述のソースコードは<?php echo count_total( 1 ) ;?>
によって「ユーザーIDが1の、食事の回数(パンの回数+ご飯の回数)」を出力するものです。
しかし、$userId
の部分がfunction count_pan(){}
とfunction count_total(){}
とで重複しているせいか、その出力ができません。
###ソースコード
php
1// パンの回数 2function count_pan( $userId ){ 3 global $wpdb; 4 5 $pans = $wpdb->get_results(" 6 //データベースからxxxなど色々取得する 7 "); 8 9 $sum = 0; 10 foreach ( $pans as $pan ) { 11 if ( $userId == $pan->user_id ){ 12 $sum = $pan->xxx; 13 } 14 } 15 return $sum; 16} 17 18// 食事の回数 19function count_total( $userId ) { 20 $count_pan = count_pan( $userId ) 21 $count_gohan = count_gohan( $userId ); 22 $total = $count_pan + $count_gohan; 23 return $total; 24} 25
このように引数が重複したケースでは、一般的にどのように対処するものでしょうか。
回答1件
あなたの回答
tips
プレビュー