php函數多參數
① php str_replace替換函數如何替換多個內容
在使用PHP的str_replace函數時,我們可以靈活地對不同參數使用數組。當只對needle使用數組時,函數會依次替換數組中的每個字元串,如str_replace(array('m','i'),'n',"my name is jim!"),結果為"ny nane ns jnn!"。
如果只對new_needle使用數組,函數將之視為一個整體進行替換,如str_replace('m',array('n','z'),"my name is jim!\n")返回"Arrayy naArraye is jiArray!"。
當只對haystack使用數組時,函數會返回一個數組,每個元素為替換後的字元串,如str_replace("m","n",array("my name is jim!","the game is over!")),結果為"ny nane is jin! the gane is over!"。
如果對needle和new_needle都使用數組,函數將按照數組的順序進行替換,如str_replace(array("m","i"),array("n","z"),"my name is jim!"),結果為"ny nane zs jzn!"。
如果needle數組比new_needle長,多餘的字元串將被替換為空串,如str_replace(array("m","i","s"),array("n","z"),"my name is jim!"),結果為"ny nane z jzn!"。
如果new_needle數組比needle長,多餘的項將被忽略,如str_replace(array("m","i"),array("n","z","x"),"my name is jim!"),結果為"ny nane zs jzn!"。
當三個參數都使用數組時,函數會分別對每個字元串進行替換,如str_replace(array("m","i"),array("n","z"),array("my name is jim!","the game is over")),結果為"ny nane zs jzn!the gane zs over"。
② php 函數參數超過3個怎麼處理合適
php 函數參數太多的話,建議以數組的形式傳過來,這樣方便取值,並且可以傳遞多個參數。示例如下:
<?php
//參數數組
$data=array('name'=>'chinawinxp','age'=>22,'oop'=>'yes');
//調用
test($data);
functiontest(array$data){
foreach($dataas$item){
echo"參數值為:".$item;
}
}