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 讲得很好~

代码已被折叠,点此展开

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

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

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

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

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

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

7年前 评论

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

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

7年前 评论

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

7年前 评论

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

7年前 评论

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

7年前 评论
fatrbaby

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

7年前 评论
chongyi

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

7年前 评论