發表文章

目前顯示的是 4月, 2018的文章

Latest

FB 別名 名稱類型

圖片
原來有這麼多種...

foreach 的 reference

<?php $arr = array(1, 2, 3, 4); foreach ($arr as &$value) {     $value = $value * 2; }// $arr is now array(2, 4, 6, 8) // without an unset($value), $value is still a reference to the last item: $arr[3] foreach ($arr as $key => $value) {     // $arr[3] will be updated with each value from $arr...     echo "{$key} => {$value} ";     print_r($arr); }// ...until ultimately the second-to-last value is copied onto the last value // output: // 0 => 2 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 2 ) // 1 => 4 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 4 ) // 2 => 6 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 6 ) // 3 => 6 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 6 ) ?> &是使用到reference 以上轉載於 http://php.net/manual/en/control-structures.foreach.php 我本來看不懂,後來找到了答案→ http://ccckaass.pixnet.net/blog/post/298283101-%5bphp%5d-foreach%E7%9A%84%26%28%E5%8F%...

PHP 之 str_pad() 和 strlen() 和 mb_strlen()

<?php $value = '王羲之'; $value_sub = substr($value,0,3); $value_strlen = strlen($value); $value_pad = str_pad($value_sub, $value_strlen, '*', STR_PAD_RIGHT); echo $value_sub; //印出 王 echo $value_strlen; //印出 9 echo $value_pad; //印出 王****** $value = 'Tyler'; $value_sub = substr($value,0,3); $value_strlen = strlen($value); $value_pad = str_pad($value_sub, $value_strlen, '*', STR_PAD_RIGHT); echo $value_sub; //印出 Tyl echo $value_strlen; //印出 5 echo $value_pad; //印出 Tyl** ?> - - - 使用strlen()每個中文字它會算成3 bytes str_pad()是將字串補成指定長度的方法~ 判斷繁體中文字的方法有mb_strlen() 會多一個編碼參數如mb_strlen($value,"utf-8")