php 函數(shù)中靜態(tài)變量使用的問(wèn)題實(shí)例分析
本文實(shí)例講述了php 函數(shù)中靜態(tài)變量使用的問(wèn)題。分享給大家供大家參考,具體如下:
<?phpfunction msg() { static $a = 0; echo $a++, ’<br />’;}msg();msg();msg();
上述代碼,分別輸出0,1,2 靜態(tài)變量$a在第一次定義并初始化后就會(huì)常駐內(nèi)存,直到腳本執(zhí)行完畢。
當(dāng)?shù)诙握{(diào)用msg()函數(shù)時(shí),這時(shí)的$a值為1,而不會(huì)變成0。
那么問(wèn)題來(lái)了,請(qǐng)看下面的一段代碼:
$data = array( array(’id’ => 1, ’title’ => ’衣服’, ’parent’ => 0), array(’id’ => 2, ’title’ => ’鞋子’, ’parent’ => 0), array(’id’ => 3, ’title’ => ’襯衫’, ’parent’ => 1), array(’id’ => 4, ’title’ => ’T恤’, ’parent’ => 1), array(’id’ => 5, ’title’ => ’運(yùn)動(dòng)鞋’, ’parent’ => 2), array(’id’ => 6, ’title’ => ’休閑鞋’, ’parent’ => 2),);$data2 = array( array(’id’ => 1, ’title’ => ’食物’, ’parent’ => 0), array(’id’ => 2, ’title’ => ’肉食’, ’parent’ => 1), array(’id’ => 3, ’title’ => ’素食’, ’parent’ => 1), array(’id’ => 4, ’title’ => ’牛肉’, ’parent’ => 2), array(’id’ => 5, ’title’ => ’面條’, ’parent’ => 3), array(’id’ => 6, ’title’ => ’饅頭’, ’parent’ => 3),);//生成樹型數(shù)據(jù)function genTree($items, $id = ’id’, $pid = ’parent’, $son = ’child’) { $tree = array(); $tmpMap = array(); foreach ($items as $item) { $tmpMap[$item[$id]] = $item; } foreach ($items as $item) { if (isset($tmpMap[$item[$pid]])) { $tmpMap[$item[$pid]][$son][] = &$tmpMap[$item[$id]]; } else { $tree[] = &$tmpMap[$item[$id]]; } } unset($tmpMap); return $tree;}//通過(guò)給定數(shù)據(jù),返回option的字符串,用于select下拉框function getOpts($data, $idArr = array(), $level = 0, $son = ’child’) { static $opt = ’’; if (!empty($data)) { foreach ($data as $k => $v) { $opt .= ’<option value=’ . $v[’id’] . (in_array($v[’id’], $idArr) ? ’ selected='true'’ : ’’) . ’>’ . str_repeat(’ ’, $level * 3) . $v[’title’] . ’</option>’; if (!empty($v[$son])) {getOpts($v[$son], $idArr, $level + 1, $son); } } } return $opt;}$data = genTree($data);echo ’<select>’;//第一次調(diào)用getOpts時(shí),沒(méi)有任何問(wèn)題。echo getOpts($data);echo ’</select>’;$data2 = genTree($data2);echo ’<select>’;//當(dāng)?shù)诙握{(diào)用時(shí),問(wèn)題就出現(xiàn)了,還留有上次的數(shù)據(jù)echo getOpts($data2);echo ’</select>’;
上述問(wèn)題,對(duì)于可能多次調(diào)用的函數(shù),不建議在內(nèi)部使用靜態(tài)變量。
修改getOpts函數(shù)如下,從函數(shù)參數(shù)引入一個(gè)外部變量的引用,來(lái)解決此問(wèn)題。
function getOpts($data, $idArr = array(), &$opt = ’’, $level = 0, $son = ’child’) { if (!empty($data)) { foreach ($data as $k => $v) { $opt .= ’<option value=’ . $v[’id’] . (in_array($v[’id’], $idArr) ? ’ selected='true'’ : ’’) . ’>’ . str_repeat(’ ’, $level * 3) . $v[’title’] . ’</option>’; if (!empty($v[$son])) {getOpts($v[$son], $idArr, $opt, $level + 1, $son); } } }}$data = genTree($data);getOpts($data, array(), $opt);echo ’<select>’;echo $opt;echo ’</select>’;$data2 = genTree($data2);getOpts($data2, array(), $opt2);echo ’<select>’;echo $opt2;echo ’</select>’;
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php常用函數(shù)與技巧總結(jié)》、《php字符串(string)用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》及《php程序設(shè)計(jì)算法總結(jié)》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章:
1. IntelliJ IDEA設(shè)置自動(dòng)提示功能快捷鍵的方法2. 詳解idea中web.xml默認(rèn)版本問(wèn)題解決3. IntelliJ IDEA導(dǎo)入jar包的方法4. IntelliJ IDEA 2020最新激活碼(親測(cè)有效,可激活至 2089 年)5. IntelliJ IDEA 統(tǒng)一設(shè)置編碼為utf-8編碼的實(shí)現(xiàn)6. asp知識(shí)整理筆記4(問(wèn)答模式)7. idea修改背景顏色樣式的方法8. jsp EL表達(dá)式詳解9. 解決ajax的delete、put方法接收不到參數(shù)的問(wèn)題方法10. 使用Python爬取Json數(shù)據(jù)的示例代碼
