我們要寫一個寫日誌的函數,首先需要了解需求,我們一般怎麼用日誌函數呢?例如,程序執行到某一步,我希望把這個變量(地址)$user_address的值打印到日誌,我們希望日誌裡是這麼寫的:
`xx-xx-xx xx:xx $user_address : 上海市楊浦區xxxxx
然後每一條日誌都要換行,都有日期時間,
假設函數名稱是log ();
我們希望這麼叫他log('
<code><span>log</span>(){ <span>$args</span> = func_get_args();<span>//获得传入的所有参数的数组</span><span>$numargs</span> = func_num_args(); <span>//参数的个数</span><span>if</span> (<span>$numargs</span> == <span>0</span>) { <span>$log</span> = <span>""</span>; } elseif (<span>$numargs</span> == <span>1</span>) { <span>$log</span> = <span>$args</span>[<span>0</span>]; } <span>else</span> { <span>$format</span> = array_shift(<span>$args</span>); <span>//分割掉函数第一个元素,并且做返回值返回,'$user_address:%s'</span><span>$log</span> = vsprintf(<span>$format</span>, <span>$args</span>); <span>//把参数代入$format中,</span> } <span>$log</span> = <span>date</span>(<span>"[Y/m/d H:i:s] "</span>) . <span>$log</span> . PHP_EOL;<span>//加上时间</span><span>$file</span> = <span>'/usr/share/nginx/html/log.log'</span>; <span>$fp</span> = <span>fopen</span>(<span>$file</span>, <span>'a'</span>); <span>fwrite</span>(<span>$fp</span>, <span>$log</span>); <span>fclose</span>(<span>$fp</span>); <span>return</span> true; } </code>
1.印出一般變數$a,
log('得到了$a的值:%s',$a );2 . 印出一個陣列$arr
log('%s',print_r( $arr,true));
').addClass('pre-numbering').hide();
$(this).addClass('has-numbering').parent().append($numbering);
for (i = 1; i
').text(i));
};
$numbering.fadeIn(1700);
});
});