用户评论:
Chris (-10-18 17:49:44)
Anotherwaytodisplayarraysisuseanarray_walk().Thiscanbeusefulinlineecho/printwhereaforeachwouldn'twork,e.g.
<?phpecho "Theseerrors:",(unset)array_walk($msgs,function($a){echo"
$a
";}),"mustbecorrected.";?>
taken from "Php Phrasebook" (-06-15 22:55:32)
$string='ThesiterunsonPHP'.phpversion();preg_match('/php((\d)\.\d\.\d+)/i',$string,$matches);print_r($matches);vprintf('Match:%s
Version%s;Major:%d.',$matches);?>
output:
Array([0]=>PHP5.2.5[1]=>5.2.5[2]=>5)
Match:PHP5.2.5Version5.2.5;Major:5.
Forpreg_match:
Ifmatchesisprovided,thenitisfilledwiththeresultsofsearch.$matches[0]willcontainthetextthatmatchedthefullpattern,$matches[1]willhavethetextthatmatchedthefirstcapturedparenthesizedsubpattern,andsoon.
badcop666 at hotmail dot com (-11-28 17:38:28)
Forblocksoftext,sprintf()isslowaccordingtomytests.
Also,havingthemappingbetweenplace-holdersandthelistofactualvariablesordatastructuresoftenmakesthiscodedifficulttoread.Buttheprintf()familyarewidelysupportedandhaveahugerangeofnicefeatures.Performanceisacoldmistressthough!
Fromanease-of-readingandmaintenance,debuggingpointofview,ImuchpreferHEREDOCand"...{$variable}..."methods.
ForablockofHTMLmarkupwithplaceholders,thefastestbyfarwas:-
?>
markupetc=$variable?>moremarkup
Mytestscomprised20runsofaloopof1millioniterationswithoutputbuffering,ditchingthebufferoneachloop.
Thetimingsrangedfromaverage2.1msec/millionrepetitionsforthe=$var?>methodupto7.6msec/millionusingprintf().
I'lltrysomebenchmarkingtoolstoo,sinceIjustwrotethismyselfanditcouldbeintroducingbias,butthey'verunondevserverswithlowload.
Hopefullyinteresting.
tehjosh at gamingg dot net (-07-29 19:19:45)
To toolofthesystem at gmail dot com:
You don't need to use output buffering with vprintf() because you can use vsprintf(), which has the same functionality as vprintf(), except that it returns the resulting string instead of outputting it.
toolofthesystem at gmail dot com (-03-27 08:42:48)
This function comes useful sometimes when trying to list information returned from MySQL:
function print_sql($query,$printf){
$sql_sql = mysql_query($query);
while($sql = mysql_fetch_row($sql_sql)){
vprintf($printf,$sql);
}
}
Unfortunately, this seems to sneak its way past output buffering when I tried creating an argument to allow it to be contained in a returned string... either that or I didn't do it right.
soylent at soylentgreens dot com (-12-10 01:49:17)
Iwantedtoachieve(somethinglike)this:
$format="A%s%s%s.\n";
$array1=Array("monkey","cow","rooster");
$array2=Array("eats","goes","crows");
$array3=Array("bananas","moo","inthemorning");
printf($format,$array1,$array2,$array3);
?>
Output:
Amonkeyeatsbananas.
Acowgoesmoo.
Aroostercrowsinthemorning.
butIcouldn'tfindanyphpfunctiontoputinforprintfthatwouldwork(vprintfcomesclose).SoIcreatedthislittlefunction(andusedittocreateaselectbox):
/*
printf_arrays(stringformat,[arrayargs[,array...]])
*/
functionprintf_arrays($format){
$args=func_get_args();
array_shift($args);//getridofformat
for($i=0;$i
$pfargs=Array();
foreach($argsas$arr)$pfargs[]=(is_array($arr)&&$arr[$i])?$arr[$i]:'';
vprintf($format,$pfargs);
}
}
$months=Array(
'01'=>'Jan',
'02'=>'Feb',
/*etc.*/
);
?>
printf_arrays('%s',array_keys($months),array_values($months))?>
Anyoneelsehaveanybetterideas?Isthereabuilt-inphpfunctionImissedthatdoesthisalready?
caleb at tekhawk dot com (-11-29 14:19:27)
i know that you can use %1$s or %3$s to select the first or third string but how can you or can you use array names to select it
something like %'user'$s $'email'$s
i tend to add things to my databases over time and this could save loads of recoding
WebMaestro (asiby at hotmail dot com) (-03-13 00:40:51)
$fruits=array(1,'banana',1,'apples',3,'oranges',2,'peaches');vprintf("Ihave%d%s,%d%s,%d%sand%d%s.",$fruits);?>
Output:
Ihave1banana,1apples,3orangesand2peaches.