1.函數並當即調用javascript
call_user_func(function(){ echo "hello,world"; });
2.上下文變量php
$context="hello,world"; call_user_func(function()use($context){ echo $context; });
3.use和$thishtml
class A { public $t; public function __construct($t="hello,world"){ $this->t=$t; } function test(){ call_user_func(function(){ echo $this->t;//php5.4+ }); } } $a=new A(); $a->test(); class Hello { private $message = "Hello world\n"; public function createClosure() { return function() { echo $this->message; }; } } class Bye { private $message = "Bye world\n"; } $hello = new Hello(); $helloPrinter = $hello->createClosure(); $helloPrinter(); // outputs "Hello world" $bye = new Bye(); $byePrinter = $helloPrinter->bindTo($bye, $bye);//like javascript apply $byePrinter(); // outputs "Bye world" $CI = $this; $callback = function () use (&$CI) { $CI->public_method(); };
4.調用一切可調用的東西html5
class Contrller{ //調用具體的action, public function __act($action){ call_user_func( array($this,$action) ); //$this->{$action}() } } class HelloContrller extends Controller{ public function index(){ } public function hello(){ } public function dance(){ } }
5.裝飾器java
//裝飾器 $dec=function($func) { $wrap=function ()use ($func) { echo "before calling you do sth\r\n"; $func(); echo "after calling you can do sth too\r\n "; }; return $wrap; }; //執行某功能的函數 $hello=function (){ echo "hello\r\n"; }; //裝飾 $hello=$dec($hello); //在其餘地方調用通過裝飾的原函數 $hello(); /*output: before calling you do sth hello after calling you can do sth too */
6.objectmysql
$obj = new stdClass; $obj->a = 1; $obj->b = 2;
7.staticios
class father { public function __construct() { // $this->init(); static::init(); } private function init() { echo "father\n"; } } class son extends father { /*public function __construct() { $this->init(); }*/ public function init() { echo "son\n"; } } $son = new son(); son::init();//son
8.list explodesql
list( , $mid) = explode(';', 'aa;bb;cc'); echo $mid; //輸出 bb
9.object array數據庫
$array = json_decode(json_encode($deeply_nested_object), true); //$array = (array) $yourObject; function object_to_array($data){ if (is_array($data) || is_object($data)) { $result = array(); foreach ($data as $key => $value) { $result[$key] = object_to_array($value); } return $result; } return $data; }
10.treejson
function displayTree($array){ $newline = "<br>"; $output = ""; foreach($array as $key => $value) { if (is_array($value) || is_object($value)) { $value = "Array()" . $newline . "(<ul>" . $this->displayTree($value) . "</ul>)" . $newline; } $output .= "[$key] => " . $value . $newline; } return $output; }
11.交換變量list($a, $b) = array($b, $a);
12.PHP中SESSION反序列化機制
修改php系列化的引擎, ini_set('session.serialize_handler', '須要設置的引擎');ini_set('session.serialize_handler', 'php_serialize');默認是以文件的方式存儲。
存儲的文件是以sess_sessionid來進行命名的,文件的內容就是session值的序列話以後的內容,name|s:6:"spoock";。name是鍵值,s:6:"spoock";是serialize("spoock")的結果。
13.php-fpm
vi /etc/php-fpm.d/www.conf
TPC socket
listen = 127.0.0.1:9000
Unix Domain Socket (Apache 2.4.9+)
listen = /var/run/php-fpm/php-fpm.sock
沒有sock文件的話先設置listen = /var/run/php-fpm/php-fpm.sock,而後重啓下php-fpm就有了
14.php和js數組區別
JS:數組屬於引用類型值,存儲在堆中https://www.zhihu.com/questio...
var a = {"Client":"jQuery","Server":"PHP"};
var b = a;
a["New"] = "Element";
console.log(b);
// 輸出 Object { Client="jQuery", Server="PHP", New="Element"}
PHP例程1:
$a = array('Client'=>'jQuery','Server'=>'PHP');
$b = $a;
$a['New'] = 'Element';
var_export($b);
//輸出 array('Client'=>'jQuery','Server'=>'PHP')
PHP例程2:
$a = array('Client'=>'jQuery','Server'=>'PHP');
$b = &$a; //引用賦值
$a['New'] = 'Element';
var_export($b);
//輸出 array('Client'=>'jQuery','Server'=>'PHP','New'=>'Element')
PHP對象默認是引用賦值,而不是值複製:
class foo {
public $bar = 'php';
}
$foo = new foo();
$tmp = $foo;//$tmp = clone $foo;值複製
$tmp->bar = 'sql';
echo $foo->bar."n"; //輸出sql
15.編碼
「中」這個漢字在UTF-8的16進制編碼是0xe4,0xb8,0xad
所以在雙引號字符串中,會被轉義爲 「中」 x開頭表示這是一個以十六進制表達的字符,就和HTML中&xe4; 同樣 PHP中的字符串就是直接本地編碼二進制存儲的
單引號字符串中,直接輸出xe4xb8xad
$str1 = "xe4xb8xad";中
$str2 = 'xe4xb8xad';xe4xb8xad
$str3 = '中';
16.數據庫鏈接
new mysqli時經過p:127.0.0.1來表示使用持久鏈接,裏面的p就是persistent持久的意思
function db() {
static $db; //靜態變量 if ($db) { return $db; } else { $db = new mysqli('p:127.0.0.1','user','pass','dbname',3306); return $db; }
}
function foo1() {
return db()->query('SELECT * FROM table1')->fetch_all();
}
function foo2() {
return db()->query('SELECT * FROM table2')->fetch_all();
}
var_export( foo1() );
var_export( foo2() );
function db() {
static $db; //靜態變量 if ($db) { return $db; } else { $db = uniqid(mt_rand(), true); return $db; }
}
function foo1() { return db(); }
function foo2() { return db(); }
echo foo1()."n";
echo foo2()."n";
17 面向對象
繼承:能夠使子類複用父類公開的變量、方法;
封裝:屏蔽一系列的細節。使外部調用時只要知道這個方法的存在;
多態:父類的方法繼承的到子類之後能夠有不一樣的實現方式;
繼承:書 & 教材 & 計算機類教材 —— 這就是現實世界的繼承關係。
封裝:手機 —— 它是封裝好的,當你使用它時,沒必要知道里面的電路邏輯。
多態:人.看(美女) & 人.看(強光) —— 參數類型不同,執行的也不同。
https://segmentfault.com/a/11...
18.數字前自動補0
$a = 1;
echo sprintf("%02d", $a);//輸出該數字,若十位、個位爲空或0,自動補零
$a = '01';
echo sprintf('%d', $a);//去0
function addZ(a){
return ('0' + a).slice(-2);
}
19.html5 使用 Content-Security-Policy 安全策略
與ios 交互使用jsbridge庫須要添加信任的frame-src
<meta http-equiv="Content-Security-Policy" content="default-src ; frame-src 'self' wvjbscheme://; style-src 'self' http://.xxx.com 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval' http://.xxx.com;">
20.php數組轉js數組
var mapRegion = <?php echo json_encode($mapRegion) ?>
21.浮點數精度 四捨六入五成雙
銀行家舍入 的方法,即:
捨去位的數值小於5時,直接捨去;
捨去位的數值大於等於6時,進位後捨去;
當捨去位的數值等於5時,分兩種狀況:5後面還有其餘數字(非0),則進位後捨去;若5後面是0(即5是最後一位),則根據5前一位數的奇偶性來判斷是否須要進位,奇數進位,偶數捨去。
49.6101 -> 50
49.499 -> 49
49.50921 -> 50
48.50921 -> 48
48.5101 -> 49
function tail($num, $fen) {
$avg = bcdiv($num, $fen, 2); //除 $tail = bcsub($num, $avg*($fen-1), 2); //減 echo $num.'='.str_repeat($avg.'+', $fen-1).$tail."\n"; echo "$num=$avg*($fen-1)+$tail\n"; return array($avg, $tail);
}
var_export(tail(100, 3));
var_export(tail(100, 6));
//輸出:
100=33.33+33.33+33.34
100=33.33*(3-1)+33.34
array (
0 => '33.33',
1 => '33.34',
)
100=16.66+16.66+16.66+16.66+16.66+16.70
100=16.66*(6-1)+16.70
array (
0 => '16.66',
1 => '16.70',
)
來自
知乎
http://stackoverflow.com/ques...
http://stackoverflow.com/ques...
https://www.quora.com/What-ar...