class MyPDOStatement extends PDOStatement { protected $_debugValues = null; protected function __construct() { // need this empty construct()! } public function execute($values=array()) { $this->_debugValues = $values; try { $t = parent::execute($values); // maybe do some logging here? } catch (PDOException $e) { // maybe do some logging here? throw $e; } return $t; } public function _debugQuery($replaced=true) { $q = $this->queryString; if (!$replaced) { return $q; } return preg_replace_callback('/:([0-9a-z_]+)/i', array($this, '_debugReplace'), $q); } protected function _debugReplace($m) { $v = $this->_debugValues[$m[1]]; if ($v === null) { return "NULL"; } if (!is_numeric($v)) { $v = str_replace("'", "''", $v); } return "'". $v ."'"; } } // have a look at http://www.php.net/manual/en/pdo.constants.php $options = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_STATEMENT_CLASS => array('MyPDOStatement', array()), ); // create PDO with custom PDOStatement class $pdo = new PDO($dsn, $username, $password, $options); // prepare a query $query = $pdo->prepare("INSERT INTO mytable (column1, column2, column3) VALUES (:col1, :col2, :col3)"); // execute the prepared statement $query->execute(array( 'col1' => "hello world", 'col2' => 47.11, 'col3' => null, )); // output the query and the query with values inserted //http://stackoverflow.com/questions/7716785/get-last-executed-query-in-php-pdo var_dump( $query->queryString, $query->_debugQuery() );
composer require nesbot/carbon use Carbon\Carbon; \Carbon\Carbon::setLocale('zh'); echo Carbon::now()->toDateTimeString(); echo Carbon::parse('2016-10-15')->toDateTimeString(); Carbon::parse('+3 days')->toDateTimeString(); echo Carbon::parse('next wednesday')->toDateTimeString(); echo Carbon::now()->modify('+15 days'); $first = Carbon::create(2012, 9, 5, 23, 26, 11); $second = Carbon::create(2012, 9, 5, 20, 26, 11, 'America/Vancouver'); var_dump($first->gt($second)); // bool(false) var_dump(Carbon::create(2012, 9, 5, 3)->between($first, $second)); // bool(true) $dt = Carbon::now(); $dt->isWeekday(); echo Carbon::now()->subDays(5)->diffForHumans(); // 5天前 echo $dt->diffForHumans($dt->copy()->addMonth()); // 1月前 echo Carbon::now()->subDays(24)->diffForHumans(); // 3周前
//簡單說就是傳入的參數支持二進制數據,包括」\0″這種在 C 中表示字符串結束的字符 $string1 = "Hello"; $string2 = "Hello\x00Hello"; echo strcoll($string1, $string2); // 返回0, 因爲是非二進制安全,誤判爲相等 echo strcmp($string1, $string2); // 返回負數
class IndexController extends Controller { /** * 腳本執行是否完成 * @var bool */ protected $complete = false; public function __construct() { register_shutdown_function([$this, 'shutdown']); } /** * 異常處理 */ public function shutdown() { if ($this->complete === false) { dump('log'); //此處應該輸出日誌並進行異常處理操做 } } }
class Util { private static $mobileSegment = [ '134', '135', '136', '137', '138', '139', '150', '151', '152', '157', '130', '131', '132', '155', '186', '133', '153', '189', ]; public function nextMobile() { $prefix = self::$mobileSegment[array_rand(self::$mobileSegment)]; $middle = mt_rand(2000, 9000); $suffix = mt_rand(2000, 9000); return $prefix . $middle . $suffix; } } //匹配手機號的正則表達式 #^(13[0-9]|14[47]|15[0-35-9]|17[6-8]|18[0-9])([0-9]{8})$# $arr = array( 130,131,132,133,134,135,136,137,138,139, 144,147, 150,151,152,153,155,156,157,158,159, 176,177,178, 180,181,182,183,184,185,186,187,188,189, ); for($i = 0; $i < 10; $i++) { $tmp[] = $arr[array_rand($arr)].' '.mt_rand(1000,9999).' '.mt_rand(1000,9999); } var_export(array_unique($tmp)); //輸出 array ( 0 => '139 9182 8973', 1 => '144 7038 6282', 2 => '182 2183 9323', 3 => '176 1226 2322', 4 => '183 1072 4890', 5 => '153 8744 2917', 6 => '152 1150 5508', 7 => '147 3404 5840', 8 => '139 3547 8652', 9 => '151 1968 2090', )
$var1 = 1; function test(){ global $var1;//global $var1;等於$var1=&$GLOBALS['var1']; unset($var1); } test(); echo $var1;// 此處輸出1 $var1 = 1; function test(){ unset($GLOBALS['var1']); } test(); echo $var1;// 此處報錯PHP Notice: Undefined variable: var1
$str1 = 12.34; $str2 = 12.35; $str3 = 12.36; echo sprintf('%.1f',$str1);//12.3 echo sprintf('%.1f',$str2);//12.3 echo sprintf('%.1f',$str3);//12.4 echo sprintf('%.1f', floor($str3));//12.3 echo sprintf('%.0f', 12.5); //12 echo sprintf('%.0f', 15.5); //16
$pdo = new PDO("mysql:host=localhost;dbname=test", "root", "root"); function getCategories(PDO $pdo, $pid = 0) { $sql = 'SELECT * FROM `category` WHERE pid=:pid'; $stmt = $pdo->prepare($sql); $stmt->bindParam(':pid', $pid, PDO::PARAM_INT); $stmt->execute(); $data = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach ($data as &$row) { $row['subs'] = getCategories($pdo, $row['id']); } return $data; } $a = getCategories($pdo); print_r($a);
public function find_children_cat($cat_id, $data) { static $tem=array(); foreach ($data as $val) { if ( $val['parent_id'] == $cat_id ) { array_push($tem, $val['cat_id']); $this->find_children_cat($val['cat_id'], $data); } } return $tem; }
//請求的時候添加 Authorization頭,值爲"Basic "+base64_encode(username+':'+password) $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($curl, CURLOPT_USERPWD, "[$username]:[$password]");
switch($value){ case null: echo 'null'; break; case '': echo '空'; break; } 當$value = ''時,switch會進入第一個case switch(true) { case null === $value : echo 'null'; break; case '' === $value: echo '空'; break; } /** * 移除字符串的BOM * * @param string $str 輸入字符串 * @return string 輸出字符串 */ function removeBOM($str) { $str_2 = substr($str, 0, 2); $str_3 = substr($str, 0, 3);//$str_2.$str{2}; $str_4 = substr($str, 0, 4);//$str_3.$str{3}; if ($str_3 == pack('CCC',0xef,0xbb,0xbf)) //utf-8 return substr($str, 3); elseif ($str_2 == pack('CC',0xfe,0xff) || $str_2 == pack('CC',0xff,0xfe)) //unicode return substr($str, 2); elseif ($str_4 == pack('CCCC',0x00,0x00,0xfe,0xff) || $str_4 == pack('CCCC',0xff,0xfe,0x00,0x00)) //utf-32 return substr($str, 4); return $str; }
$arr1 = [ ['tracking1','abc@qq.com','80'], ['tracking1','abc@qq.com','50'], ['tracking2','efg@qq.com','60'], ['tracking2','efg@qq.com','30'], ]; $arr2 = []; foreach ($arr1 as $data) { list($account,$mail,$val) = $data; isset($arr2[$account.$mail]) || $arr2[$account.$mail]=[$account,$mail,[]]; array_push($arr2[$account.$mail][2],$val); } $arr2 = array_values($arr2); var_dump($arr2); $arr = [['tracking1','abc@qq.com','80'], ['tracking1','abc@qq.com','50'], ['tracking2','efg@qq.com','60'], ['tracking2','efg@qq.com','30']]; $finalArr = [[[]]]; $mailArr =[]; foreach ($arr as $k=>$v){ $mailKey = array_search($v[1],$mailArr); if($mailKey!==false){ array_push($finalArr[$mailKey][2],$v[2]); }else{ $finalArr[$k] = $v; $finalArr[$k][2] = [$v[2]]; $mailArr[$k]=$v[1]; } } $finalArr = array_values($finalArr); var_dump($finalArr);
$items = [ 'a > 1', 'b > 0', 'abc' => 'c > 1' ]; $subject = '#0 and #1 or (#0) and #abc #nooo'; echo preg_replace_callback('/#([a-z0-9_]+)/i', function($v) use ($items){ return isset($items[$v[1]]) ? $items[$v[1]] : $v[0]; }, $subject);//a > 1 and b > 0 or (a > 1) and c > 1 #nooo
$data = ['a1' => sprintf('0b%06b', 0b000100)]; // 轉成JSON後: {"a1": "0b000100"}
php
echo sprintf('%.1f', floor($str));
sprintf('%.1f',12.35);//12.3
sprintf('%.1f',12.36);//12.4
number_format(12.35,1,'.','')//12.4
number_format(12.36,1,'.','')//12.4html
$obj='{"order_id":213477815351175,"buyer":100001169269154}'; $obj=$this->json_decode($obj,TRUE); print_r($obj); Array ( [order_id] => 2.1347781535118E+14 [buyer] => 1.0000116926915E+14 ) $obj='{"order_id":213477815351175,"buyer":100001169269154}'; $obj=$this->json_decode($obj,TRUE); foreach ($obj as $key=>$val){ $obj[$key]=number_format($val,0,'',''); } print_r($obj); Array ( [order_id] => 213477815351175 [buyer] => 100001169269154 )
//http://blog.csdn.net/fdipzone/article/details/51794055 function getNumGroups($var, $num){ // 數量不正確 if($var<$num){ return array(); } $total = 0; $result = array(); for($i=1; $i<$num; $i++){ $tmp = mt_rand(1, $var-($num-$i)-$total); $total += $tmp; $result[] = $tmp; } $result[] = $var-$total; return $result; } $result = getNumGroups(100, 3); print_r($result); Array ( [0] => 42 [1] => 25 [2] => 33 )
test.php echo date('Y-m-d H:i:s').PHP_EOL; * * * * * php /Users/fdipzone/test.php >> /Users/fdipzone/test.log * * * * * sleep 30; php /Users/fdipzone/test.php >> /Users/fdipzone/test.log
function dosth($callback){ call_user_func($callback); } function callback(){ echo 'do sth callback'; } dosth('callback'); function dosth(callable $callback){ call_user_func($callback); } dosth('abc');//提示錯誤:TypeError: Argument 1 passed to dosth() must be callable
/** * 生成0~1隨機小數http://blog.csdn.net/fdipzone/article/details/52829930 * @param Int $min * @param Int $max * @return Float */ function randFloat($min=0, $max=1){ return $min + mt_rand()/mt_getrandmax() * ($max-$min); } // 獲取microtime function get_microtime(){ list($usec, $sec) = explode(' ', microtime()); return (float)$usec + (float)$sec; } lcg_value(); //lcg_value()執行速度快,但隨機效果不及基於mt_rand()與mt_getrandmax()算法實現 header('content-type: image/png'); $im = imagecreatetruecolor(512, 512); $color1 = imagecolorallocate($im, 255, 255, 255); $color2 = imagecolorallocate($im, 0, 0, 0); for($y=0; $y<512; $y++){ for($x=0; $x<512; $x++){ $rand = randFloat(); if(round($rand,2)>=0.5){ imagesetpixel($im, $x, $y, $color1); }else{ imagesetpixel($im, $x, $y, $color2); } } } imagepng($im); imagedestroy($im); /** * 檢查鏈接是否可用 * @param Link $dbconn 數據庫鏈接 * @return Boolean */ function pdo_ping($dbconn){ try{ $dbconn->getAttribute(PDO::ATTR_SERVER_INFO); } catch (PDOException $e) { if(strpos($e->getMessage(), 'MySQL server has gone away')!==false){ return false; } } return true; }
function test() { echo count(debug_backtrace()) . "\n"; } function test2() { test(); } test(); //輸出1 test2(); //輸出2 惰加載 只是給$this->['config']一個匿名函數,當你要用到的時候,纔會進行new Config($config)的操做 public function __construct($config) { parent::__construct(); $this['config'] = function () use ($config) { return new Config($config); };
'email' => 'required_without:phone', 'phone' => 'required_without:email', set_time_limit 能夠控制秒級的最大執行時間,一個500毫秒的超時 declare(ticks=1); $start = microtime(true); register_tick_function(function () use ($start) { (microtime(true) - $start < 0.5) or die("timeout\n"); }); function a() { echo "do some work\n"; usleep(600000); echo "do another work\n"; } a();
//設置post的數據 $post = array ( 'email' => '帳戶', 'pwd' => '密碼' ); //登陸地址 $url = "登錄地址"; //設置cookie保存路徑 $cookie = dirname(__FILE__) . '/cookie.txt'; //登陸後要獲取信息的地址 $url2 = "登錄後要獲取信息的地址"; //模擬登陸 login_post($url, $cookie, $post); //獲取登陸頁的信息 $content = get_content($url2, $cookie); //刪除cookie文件 @ unlink($cookie); var_dump($content); //模擬登陸 function login_post($url, $cookie, $post) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 0); curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post)); curl_exec($curl); curl_close($curl); } //登陸成功後獲取數據 function get_content($url, $cookie) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); $rs = curl_exec($ch); curl_close($ch); return $rs; }
class Base { public function log() { // 目標類,輸出:A/C echo static::class; // 基類,輸出:Base //echo __CLASS__; echo self::class; } } class A extends Base { public function log1() { echo self::class; } } class C extends A { public function log2() { echo self::class; } } //self 指向基類 Fruit,也就是 __CLASS__ 的類 //static、$this 指向最終new的類 Apple $a = new A();$c = new C(); $a->log(); //輸出 A Base $c->log(); //輸出 C Base $c->log1(); //輸出 A $c->log2(); //輸出 C
CREATE TABLE sessions ( user_id int(10) unsigned NOT NULL, session text NOT NULL, md5 char(32) NOT NULL, PRIMARY KEY (user_id) ) ENGINE=MEMORY DEFAULT CHARSET=utf8; 其中: user_id存儲的是用戶ID,做爲主鍵. session存儲的是用戶的會話數組通過serialize或json_encode後的字符串. md5存儲的是session字段的MD5值,用於實現Check And Set版本號樂觀鎖: --讀取會話 SELECT session, md5 --寫入會話時須要用到這裏查出來的md5,就是下面的$last_md5 FROM sessions WHERE user_id = $user_id --寫入會話 UPDATE sessions SET session = $str, md5 = md5($str) WHERE user_id = $user_id AND md5 = $last_md5 --檢查MD5,確保session字段沒有被修改過