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...