sprintf函数减少字符串拼接错误

时间:2023-11-19 21:49:26
     $return_string="";
     foreach($cat_list as $value){

         $return_string .= sprintf('<dd><a href="category.php?id=%s">%s</a></dd>',$value['cat_id'],$value['cat_name']);

     }

简单无脑,整串字符串塞进函数第一个参数,然后变量换成%d、%s啊什么的,然后第二个参数那里填变量,就会自动拼接啦,就不用自己老是拼接字符串错误啦!!!

 <?php
 $number = 2;
 $str = "Shanghai";
 $txt = sprintf("There are %u million cars in %s.",$number,$str);
 echo $txt;
 ?>