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;
}
$temp = 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_export($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 也是很少用的,不过背后也是可以实现超级厉害的东西,反正我就看不懂咯,希望有大神讲解一下下~

乐观 自信 爱
本帖已被设为精华帖!
本帖由 Summer 于 6年前 加精
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 9
chenyuanqi

一眼看下去,感觉这里就不对了, 输出真的是 true?

var_dump(false and true);// 与   true
6年前 评论

@chenyuanqi 哈哈, 注释是批量复制的 :smile: , 结果忘了改了,,,不过想要说明的是PHP支持and or xor运算符~~

6年前 评论

2 <=> 1 一般用在 usort 的闭包函数比较里
请问下,这个用什么查看到的

/* 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  */
6年前 评论

“<=>” 结合比较运算符
"??" null合并运算符
都是7开始支持的

6年前 评论

@mingyun 这个是在用户注解里面看到的, 我觉得解析比较清楚 就摘抄到这里了 , 在手册的位置是语言参考->引用解析->取消引用 , 我觉得是那位User notes的作者画出来的 , 不是用工具打印出来的

6年前 评论

带上版本说明,是不是会更好。

6年前 评论

学习了 刚知道 file_get_content 还可以 post 厉害了...

6年前 评论
fatrbaby

好巧哦,帖子里的东西我都知道。

6年前 评论
chongyi

@736713830 参考 PHP 文档中对上下文(Context)概念的解释,这只是其中的一个

6年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!