PHP手册拾遗

最近花了一个星期翻了一遍PHP手册, 算是一次拾遗~

运算符

and/or/xor运算符

PHP还可以用and/or/xor的逻辑运算符~

var_dump(false and true);// 与   false
var_dump(false or true); // 或   true
var_dump(true xor false);// 异或 true

会不会因为可移植性的问题,而不推荐使用....?

<=>运算符

$smaller = 1 <=> 2;
$equal   = 1 <=> 1;
$lager   = 2 <=> 1;
var_dump($smaller);
var_dump($equal);
var_dump($lager);
//-1 小于 0等于 1 大于

那么小于等于,大于等于怎样表示?

$equalOrSamller = (1 <=> 2) != 1;
$equalOrLager   = (1 <=> 2) != -1;
//小于等于 == 不大于 , 大于等于 == 不小于

不过这个不知道怎样用应该什么时候使用捏

??运算符

PHP7新出的??运算符,再也不用使用一个小函数来设置默认值了

function defaultValue(&$var,$default){
  if(!isset($var))
    return $default;
  return $var;
}
defaultValue($temp,'a');

现在可以和JS类似的写法~

$var = $args ?? 'default value';

file_get_content也可以发送Post请求

看了手册才发现,其实file_get_content一样可以post,设置cookies,感觉比Curl直观多了~当然参数不是很多,相比于Curl

$opts = array(
  'http' =>array(
    'proxy' => null,
    'method' => null,
    'header' => null,
    'timeout' => null,
    'content' => null,
    'user_agent' => null,
    'max_redirects' => null,
    'ignore_errors' => null,
    'request_fulluri' => null,
    'follow_location' => null,
    'protocol_version' => null
  )
);

$context = stream_context_create($opts);
$result = file_get_contents('http://example.com/submit.php', false, $context);
//或者
$stream = fopen('http://example.com/submit.php', 'r', false, $context);
var_dump(stream_get_meta_data($stream));//响应报文的信息
var_dump(stream_get_contents($stream));
fclose($stream);
stream_meta_data.png

一样可以创建之后修改options

stream_context_set_option($context,$opts);

来个小小性能对比,循环100次get http://www.baidu.com

重复循环整个的.png

不过把循环搬到发送那一步,就是变成这样,curl的优势体现出来了~~

Curl复用优势.png
仅仅重复发送的.png

看了手册发现原来file_get_content一样是可以post请求的,所以还是看官网手册自己写Demo实测比较好~~

详细的使用方式去看手册吧file_get_content/fopen其实PHP所封装的协议应该都可以用的,不仅仅是http(s),反正去看手册就是啦

详细点的可以去爬手册.png

生成器

它的workflow我的理解方式是这样的~

function genaretor(){
  $receive1 = yield 'send out 1';
  yield $receive1;
  $receive2 = yield 'send out 2';
  // var_dump($this);
  // $this->next();这样不行~就是说只能外部控制咯~
  //Fatal error: Uncaught Error: Using $this when not in object context
  yield $receive2;//入口和出口绑定一起了~可能有时候只想出,不想进~
}

$gen = genaretor();
var_dump($gen);//class Generator#4 (0) {}
var_dump($gen);//Generator::__set_state(array()) , 记得魔术方法里面有__set_state()
$gen->rewind();
var_dump($gen->current());//send out 1

// $gen->next();
// $gen->rewind();
//Fatal error: Uncaught Exception: Cannot rewind a generator that was already run

var_dump($gen->send('send in 1'));//send in 1
var_dump($gen->current());//send in 1

var_dump($gen->next());//null next不会返回数据
var_dump($gen->current());//send out 2

var_dump($gen->send('send in 2'));
$gen->next();
var_dump($gen->current());//send out 2

这家伙背后是协程的实现....请直接移步鸟哥的翻译优化版

不过我现在需要处理的数据结构也不是很复杂,,,,希望大牛举几个例子呗~~

ArrayAccess接口

这个可以理解为[]的重载为对象的数据获取方式增添的另一种方式吧

class arrayLikeObj implements ArrayAccess {
  private $container = array();
  public function __construct($arr) {
    $this->container = $arr;
  }
  public function offsetSet($offset, $value) {
    if (is_null($offset)) {
        $this->container[] = $value;
    } else {
        $this->container[$offset] = $value;
    }
  }
  public function offsetExists($offset) {
    return isset($this->container[$offset]);
  }
  public function offsetUnset($offset) {
    unset($this->container[$offset]);
  }
  public function offsetGet($offset) {
    return isset($this->container[$offset]) ? $this->container[$offset] : null;
  }
}

$obj = new arrayLikeObj(array(
  "one"   => 1,
  "two"   => 2,
  "three" => 3,
));

var_dump($obj[1]);

反正就是数组运算符[]的重载咯~只是和其他语言的实现的方式不懂而已,不知道怎样实现其他运算符的重载比如< == >

还有命名空间

这个只是我自己用得比较少,还是贴在这啦~

只要搞懂手册这个寻址规则就没问题了

命名空间规则.png

发现的拓展

发现一个v8js(V8 Javascript Engine Integration)的扩展,不知道为什么惊喜了一晚上,不过到另一天又想不到有什么用途,爬虫时候实现js跳转?....

还有FANN (Fast Artificial Neural Network),之前还看到PHP-ML, 感觉PHP还是挺时髦的啊~

对对, 还有关于PHP的引用

取消引用那里的user notes讲得很好~

/* Imagine this is memory map
  ______________________________
 |pointer | value | variable                |
  --------------------------------
 |   1     |  NULL  |         ---           |
 |   2     |  NULL  |         ---           |
 |   3     |  NULL  |         ---           |
 |   4     |  NULL  |         ---           |
 |   5     |  NULL  |         ---           |
 ------------------------------------
 Create some variables   */
$a=10;
$b=20;
$c=array ('one'=>array (1, 2, 3));
/* Look at memory
  _______________________________
 |pointer | value |       variable's        |
  -----------------------------------
 |   1     |  10      |      $a             |
 |   2     |  20      |      $b             |
 |   3     |  1       |      $c['one'][0]   |
 |   4     |  2       |      $c['one'][1]   |
 |   5     |  3       |      $c['one'][2]   |
 ------------------------------------
 do  */
$a=&$c['one'][2];
/* Look at memory
  _______________________________
 |pointer | value |       variable's        |
  -----------------------------------
 |   1     |  NULL    |      ---            |  //value of  $a is destroyed and pointer is free
 |   2     |  20      |      $b             |
 |   3     |  1       |      $c['one'][0]   |
 |   4     |  2       |      $c['one'][1]   |
 |   5     |  3       |  $c['one'][2]  ,$a  | // $a is now here
 ------------------------------------
 do  */
$b=&$a;  // or  $b=&$c['one'][2]; result is same as both "$c['one'][2]" and "$a" is at same pointer.
 /* Look at memory
  _________________________________
 |pointer | value |       variable's            |
  --------------------------------------
 |   1     |  NULL    |       ---               |  
 |   2     |  NULL    |       ---               |  //value of  $b is destroyed and pointer is free
 |   3     |  1       |      $c['one'][0]       |
 |   4     |  2       |      $c['one'][1]       |
 |   5     |  3       |$c['one'][2]  ,$a , $b   |  // $b is now here
 ---------------------------------------
 next do */
unset($c['one'][2]);
/* Look at memory
  _________________________________
 |pointer | value |       variable's            |
  --------------------------------------
 |   1     |  NULL    |      ---                |  
 |   2     |  NULL    |      ---                |  
 |   3     |  1       |      $c['one'][0]       |
 |   4     |  2       |      $c['one'][1]       |
 |   5     |  3       |      $a , $b            | // $c['one'][2]  is  destroyed not in memory, not in array
 ---------------------------------------
 next do   */
$c['one'][2]=500;    //now it is in array
 /* Look at memory
  _________________________________
 |pointer | value |       variable's            |
  --------------------------------------
 |   1     |  500     |      $c['one'][2]       |  //created it lands on any(next) free pointer in memory
 |   2     |  NULL    |       ---               |  
 |   3     |  1       |      $c['one'][0]       |
 |   4     |  2       |      $c['one'][1]       |
 |   5     |  3       |      $a , $b            | //this pointer is in use
 ---------------------------------------
 lets tray to return $c['one'][2] at old pointer an remove reference $a,$b.  */
$c['one'][2]=&$a;
 unset($a);
 unset($b);   
/* look at memory
  _________________________________
 |pointer | value |       variable's           |
  --------------------------------------
 |   1     |  NULL    |       ---               |  
 |   2     |  NULL    |       ---               |  
 |   3     |  1       |      $c['one'][0]       |
 |   4     |  2       |      $c['one'][1]       |
 |   5     |  3       |      $c['one'][2]       | //$c['one'][2] is returned, $a,$b is destroyed
 --------------------------------------- ?>
 I hope this helps. 

有一些看了但是过一天就没印象了,看手册真的很~~

想起来一个,declare 也是很少用的,不过背后也是可以实现超级厉害的东西,反正我就看不懂咯,希望有大神讲解一下下~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,100评论 18 139
  • 一、php可以做什么 php是一种可以在服务器端运行的编程语言,可以运行在Web服务器端。 php是一门后台编程语...
    空谷悠阅读 3,016评论 4 97
  • 转载自:http://www.cnblogs.com/txw1958/archive/2013/01/19/286...
    php_bruce阅读 1,707评论 1 5
  • php.ini设置,上传大文件: post_max_size = 128Mupload_max_filesize ...
    bycall阅读 6,648评论 3 64
  • 现在是凌晨5点,窗外刮着台风,呼啦呼啦地响。失眠睡不着就会胡思乱想。不由得想起小时候躲台风的事。那时候,因为学校宿...
    玲珑书语阅读 610评论 0 51