-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.xml
150 lines (69 loc) · 65.1 KB
/
search.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title>使用反射实现依赖 自动注入</title>
<link href="/2019/05/14/%E4%BD%BF%E7%94%A8%E5%8F%8D%E5%B0%84%E5%AE%9E%E7%8E%B0%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5/"/>
<url>/2019/05/14/%E4%BD%BF%E7%94%A8%E5%8F%8D%E5%B0%84%E5%AE%9E%E7%8E%B0%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5/</url>
<content type="html"><![CDATA[<p>以laravel框架为例, 很多地方出现如下代码:</p><p>声明UserService类, 同时构造一个run()方法<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserService</span></span></span><br><span class="line"><span class="class"></span>{</span><br><span class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">run</span><span class="params">()</span> </span>{</span><br><span class="line"><span class="keyword">echo</span> <span class="string">"i 'm run func()"</span>;</span><br><span class="line">}</span><br><span class="line">}</span><br></pre></td></tr></table></figure></p><p>声明个基类, 实例化时, 同时注入UserService类<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">BaseController</span></span></span><br><span class="line"><span class="class"></span>{</span><br><span class="line"> <span class="keyword">protected</span> $userService;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">__construct</span><span class="params">(UserService $userService)</span> </span>{</span><br><span class="line"> <span class="comment">//依赖注入服务</span></span><br><span class="line"> <span class="keyword">$this</span> -> userService = $userService;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure></p><p>ok, 前置条件已准备完毕。现在创建个控制器, 继承上面的BaseController来使用依赖</p><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserController</span> <span class="keyword">extends</span> <span class="title">BaseController</span></span></span><br><span class="line"><span class="class"></span>{</span><br><span class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">index</span><span class="params">()</span> </span>{</span><br><span class="line"><span class="keyword">$this</span>->userService->run();</span><br><span class="line">}</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>好了可以使用了, 浏览器输入<a href="http://localhost/user/index" target="_blank" rel="noopener">http://localhost/user/index</a> 就能调用UserService类的run()方法, 这里甚至不需要去实例化BaseController类, </p><h2 id="不需要给构造函数传参-因为框架内部使用了反射-帮你自动实例化对象-自动传参了。"><a href="#不需要给构造函数传参-因为框架内部使用了反射-帮你自动实例化对象-自动传参了。" class="headerlink" title="不需要给构造函数传参, 因为框架内部使用了反射 帮你自动实例化对象, 自动传参了。"></a>不需要给构造函数传参, 因为框架内部使用了<a href="https://www.php.net/manual/en/book.reflection.php" target="_blank" rel="noopener">反射</a> 帮你自动实例化对象, 自动传参了。</h2><p>下面自己实现一个类似的功能, 新建几个类:</p><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">User</span></span></span><br><span class="line"><span class="class"></span>{</span><br><span class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">userInfo</span><span class="params">()</span> </span>{</span><br><span class="line"><span class="keyword">echo</span> <span class="string">'user info'</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">getAge</span><span class="params">()</span> </span>{</span><br><span class="line"><span class="keyword">echo</span> <span class="number">77</span>;</span><br><span class="line">}</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">include_once</span> <span class="string">'BaseController.php'</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Worker</span> <span class="keyword">extends</span> <span class="title">BaseController</span></span></span><br><span class="line"><span class="class"></span>{</span><br><span class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">getAge</span><span class="params">()</span> </span>{</span><br><span class="line"><span class="keyword">$this</span>->user->getAge();</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">getUserInfo</span><span class="params">()</span> </span>{</span><br><span class="line"><span class="keyword">$this</span>->user->userInfo();</span><br><span class="line">}</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">include</span> <span class="string">'User.php'</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">BaseController</span></span></span><br><span class="line"><span class="class"></span>{</span><br><span class="line"><span class="keyword">protected</span> $user;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">__construct</span><span class="params">(User $user)</span> </span>{</span><br><span class="line"><span class="keyword">$this</span>->user = $user;</span><br><span class="line">}</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>好了, 然后在构造个简单的路由, 在里面实现自动注入,传参<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">include</span> <span class="string">'Worker.php'</span>;</span><br><span class="line"></span><br><span class="line">$c = $_GET[<span class="string">'c'</span>];</span><br><span class="line">$a = $_GET[<span class="string">'a'</span>];</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (class_exists($c)) {</span><br><span class="line"><span class="keyword">if</span> (method_exists($c, $a)) {</span><br><span class="line"><span class="comment">//获得路由$c类的反射实例</span></span><br><span class="line">$construct = <span class="keyword">new</span> ReflectionClass($c);</span><br><span class="line"><span class="comment">//获取路由$c类的父类反射实例</span></span><br><span class="line">$baseConstruct = <span class="keyword">new</span> ReflectionClass($construct->getParentClass()->name);</span><br><span class="line"><span class="comment">//获取A类父类的构造函数参数</span></span><br><span class="line">$params = $baseConstruct->getConstructor()->getParameters();</span><br><span class="line"></span><br><span class="line">$paramList = [];</span><br><span class="line"> <span class="keyword">foreach</span> ($params <span class="keyword">as</span> $value) {</span><br><span class="line"> $tmp = $value->getClass()->name;</span><br><span class="line"> $paramList[] = <span class="keyword">new</span> $tmp();</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">/*</span></span><br><span class="line"><span class="comment"> * 给构造函数注入参数,因子类没有构造函数,所以参数会注入到父类的构造函数中.</span></span><br><span class="line"><span class="comment"> * 这里就会实例化传入的路由类的基类,并且把参数自动注入到基类中</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"> $obj = $construct->newInstanceArgs($paramList);</span><br><span class="line"> $obj->$a();</span><br><span class="line">}</span><br><span class="line">}</span><br></pre></td></tr></table></figure><br>浏览器输入localhost/Route.php?c=Worker&a=getAge 就会打印出 77</p>]]></content>
</entry>
<entry>
<title>使用socket, 实现简单的聊天</title>
<link href="/2019/04/23/%E4%BD%BF%E7%94%A8socket-%E5%AE%9E%E7%8E%B0%E7%AE%80%E5%8D%95%E7%9A%84%E8%81%8A%E5%A4%A9/"/>
<url>/2019/04/23/%E4%BD%BF%E7%94%A8socket-%E5%AE%9E%E7%8E%B0%E7%AE%80%E5%8D%95%E7%9A%84%E8%81%8A%E5%A4%A9/</url>
<content type="html"><![CDATA[<p><a href="http://rango.swoole.com/archives/date/2013/01" target="_blank" rel="noopener">原文地址</a><br>看完后有所感悟,所以添加了些东东</p><p>先看效果图<br><img src="https://sign-super.oss-cn-hangzhou.aliyuncs.com/shudun/chatslef.jpg" alt=""></p><p>是不是很简陋, 这样已经可以了。要想完善就慢慢的花时间改吧<br>实现思路, 首先创建个主进程 SockerServer.php, 这样命名或许有点欠妥。但是我仍然暂理解为server<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?php</span></span><br><span class="line"><span class="keyword">if</span>(($sock = socket_create(AF_INET, SOCK_STREAM, <span class="number">0</span>)) === <span class="keyword">false</span>) {</span><br><span class="line"> <span class="keyword">echo</span> <span class="string">"failed to create socket: "</span>.socket_strerror($sock) . PHP_EOL;</span><br><span class="line"> <span class="keyword">die</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">$address = <span class="string">'127.0.0.1'</span>;</span><br><span class="line">$port = <span class="number">88</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(($ret = socket_bind($sock, $address, $port)) < === <span class="keyword">false</span>) {</span><br><span class="line"> <span class="keyword">echo</span> <span class="string">"failed to bind socket: "</span>.socket_strerror($ret) . PHP_EOL;</span><br><span class="line"> <span class="keyword">die</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>( ( $ret = socket_listen($sock, <span class="number">0</span> ) ) === <span class="keyword">false</span> ) {</span><br><span class="line"> <span class="keyword">echo</span> <span class="string">"failed to listen to socket: "</span>.socket_strerror($ret) . PHP_EOL;</span><br><span class="line"> <span class="keyword">die</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span> (<span class="keyword">true</span>) {</span><br><span class="line"> $conn = socket_accept($sock); <span class="comment">//主进程执行到此处,便不会往下执行. 它在等待接收新的socket.</span></span><br><span class="line"> <span class="comment">//如果有socket连接, 便fork一个 子进程</span></span><br><span class="line"> $fork = pcntl_fork();</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span>($fork == <span class="number">0</span>)</span><br><span class="line"> {</span><br><span class="line"> $recv = socket_read($conn, <span class="number">8192</span>);</span><br><span class="line"> <span class="keyword">if</span> ($recv) {</span><br><span class="line"> <span class="keyword">list</span>($name, $msg) = explode(<span class="string">"|"</span>, $recv);</span><br><span class="line"> <span class="keyword">echo</span> <span class="string">"{$name} : {$msg}"</span>;</span><br><span class="line"> <span class="keyword">die</span>;</span><br><span class="line"> }</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> socket_close($conn);</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><br>这样就可以了, 然后创建个 SockerClient.php</p><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">$address = <span class="string">'127.0.0.1'</span>;</span><br><span class="line">$port = <span class="number">88</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">echo</span> <span class="string">"insert your name:"</span>; <span class="comment">//感觉insert比input合适</span></span><br><span class="line">$uname = fgets(STDIN);</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span>(<span class="keyword">true</span>) {</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (($createSock = socket_create(AF_INET, SOCK_STREAM, <span class="number">0</span>)) === <span class="keyword">false</span>) {</span><br><span class="line"> <span class="keyword">echo</span> <span class="string">"create error :"</span> . socket_strerror($createSock);</span><br><span class="line"> <span class="keyword">die</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (($sockCon = socket_connect($createSock, $address, $port)) === <span class="keyword">false</span>) {</span><br><span class="line"> <span class="keyword">echo</span> <span class="string">"socket connect error: "</span>. socket_strerror($sockCon);</span><br><span class="line"> <span class="keyword">die</span>;</span><br><span class="line">}</span><br><span class="line"> <span class="keyword">echo</span> <span class="string">"enter msg: "</span>;</span><br><span class="line"> $msg = fgets(STDIN);</span><br><span class="line"> socket_write($createSock, $uname . <span class="string">'|'</span> . $msg);</span><br><span class="line"> socket_close($createSock);</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
</entry>
<entry>
<title>go笔记 平台之间的交叉编译</title>
<link href="/2019/02/28/go%E7%AC%94%E8%AE%B0-%E5%B9%B3%E5%8F%B0%E4%B9%8B%E9%97%B4%E7%9A%84%E4%BA%A4%E5%8F%89%E7%BC%96%E8%AF%91-1/"/>
<url>/2019/02/28/go%E7%AC%94%E8%AE%B0-%E5%B9%B3%E5%8F%B0%E4%B9%8B%E9%97%B4%E7%9A%84%E4%BA%A4%E5%8F%89%E7%BC%96%E8%AF%91-1/</url>
<content type="html"><![CDATA[<p>mac编译 linux, window64位 可执行程序<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">CGO_ENABLED=<span class="number">0</span> GOOS=linux GOARCH=amd64 go build main.go</span><br><span class="line">CGO_ENABLED=<span class="number">0</span> GOOS=windows GOARCH=amd64 go build main.go</span><br></pre></td></tr></table></figure><br>linux编译 mac, windows64位 可执行程序<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">CGO_ENABLED=<span class="number">0</span> GOOS=darwin GOARCH=amd64 go build main.go</span><br><span class="line">CGO_ENABLED=<span class="number">0</span> GOOS=windows GOARCH=amd64 go build main.go</span><br></pre></td></tr></table></figure></p><p>windows编译 Mac, Linux64位可执行程序<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">SET CGO_ENABLED=<span class="number">0</span></span><br><span class="line">SET GOOS=darwin</span><br><span class="line">SET GOARCH=amd64</span><br><span class="line">go build main.go</span><br><span class="line"></span><br><span class="line">SET CGO_ENABLED=<span class="number">0</span></span><br><span class="line">SET GOOS=linux</span><br><span class="line">SET GOARCH=amd64</span><br><span class="line">go build main.go</span><br></pre></td></tr></table></figure></p>]]></content>
</entry>
<entry>
<title>php实现检测域名是否被墙</title>
<link href="/2018/12/24/php%E5%AE%9E%E7%8E%B0%E6%A3%80%E6%B5%8B%E5%9F%9F%E5%90%8D%E6%98%AF%E5%90%A6%E8%A2%AB%E5%A2%99/"/>
<url>/2018/12/24/php%E5%AE%9E%E7%8E%B0%E6%A3%80%E6%B5%8B%E5%9F%9F%E5%90%8D%E6%98%AF%E5%90%A6%E8%A2%AB%E5%A2%99/</url>
<content type="html"><![CDATA[<p>首先准备两台服务器,普通服务器一台, 能科学上网服务器一台.<br>本人环境 php version 7.2 mysql5.7</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">大概思路:境内主要发起http请求,如果响应失败,则尝试到境外服务器请求</span><br></pre></td></tr></table></figure><p>存储结构<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">SET NAMES utf8mb4;</span><br><span class="line">SET FOREIGN_KEY_CHECKS = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">DROP TABLE <span class="keyword">IF</span> EXISTS `tbl_url`;</span><br><span class="line">CREATE TABLE `tbl_url` (</span><br><span class="line"> `id` int(<span class="number">11</span>) NOT <span class="keyword">NULL</span> AUTO_INCREMENT,</span><br><span class="line"> `in` tinyint(<span class="number">2</span>) <span class="keyword">DEFAULT</span> <span class="keyword">NULL</span>,</span><br><span class="line"> `out` tinyint(<span class="number">2</span>) <span class="keyword">DEFAULT</span> <span class="keyword">NULL</span>,</span><br><span class="line"> `note` varchar(<span class="number">255</span>) <span class="keyword">DEFAULT</span> <span class="keyword">NULL</span>,</span><br><span class="line"> `url` varchar(<span class="number">255</span>) <span class="keyword">DEFAULT</span> <span class="keyword">NULL</span>,</span><br><span class="line"> `created_at` datetime <span class="keyword">DEFAULT</span> <span class="keyword">NULL</span>,</span><br><span class="line"> `updated_at` datetime <span class="keyword">DEFAULT</span> <span class="keyword">NULL</span>,</span><br><span class="line"> PRIMARY KEY (`id`)</span><br><span class="line">) ENGINE=InnoDB AUTO_INCREMENT=<span class="number">13</span> <span class="keyword">DEFAULT</span> CHARSET=utf8;</span><br><span class="line"></span><br><span class="line">SET FOREIGN_KEY_CHECKS = <span class="number">1</span>;</span><br></pre></td></tr></table></figure></p><p>普通服务器代码<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">try</span> {</span><br><span class="line"> $pdo = <span class="keyword">new</span> PDO(</span><br><span class="line"> <span class="string">'mysql:dbname=dbname;host=127.0.0.1;charset=UTF8'</span>, <span class="string">'username'</span>, <span class="string">'password'</span></span><br><span class="line"> );</span><br><span class="line">} <span class="keyword">catch</span> ( <span class="keyword">Exception</span> $ex ) {</span><br><span class="line"> <span class="keyword">exit</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">$update = <span class="function"><span class="keyword">function</span><span class="params">( $in, $out )</span> <span class="title">use</span> <span class="params">( $pdo, &$id )</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> <span class="number">1</span> === $pdo->exec( sprintf(</span><br><span class="line"> <span class="string">'UPDATE `tbl_url` SET `in` = %d, `out` = %d, `updated_at` = NOW() WHERE `id` = %d LIMIT 1'</span>, $in, $out, $id</span><br><span class="line"> ) );</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line">curl_setopt_array( $ch = curl_init(), [</span><br><span class="line"> CURLOPT_RETURNTRANSFER => <span class="keyword">true</span>,</span><br><span class="line"> CURLOPT_TIMEOUT => <span class="number">5</span>,</span><br><span class="line">]);</span><br><span class="line"></span><br><span class="line"><span class="keyword">foreach</span>( $pdo->query(</span><br><span class="line"> <span class="string">'SELECT `id`, `url` FROM `tbl_url` WHERE 1'</span></span><br><span class="line">)->fetchAll( PDO::FETCH_KEY_PAIR ) <span class="keyword">as</span> $id => $url ) {</span><br><span class="line"></span><br><span class="line"> curl_setopt( $ch, CURLOPT_URL, <span class="string">'http://科学上网服务器地址/?url='</span> . base64_encode( $url ) );</span><br><span class="line"></span><br><span class="line"> <span class="keyword">switch</span>( curl_exec( $ch ) ) {</span><br><span class="line"></span><br><span class="line"> <span class="keyword">case</span> <span class="number">1</span> :</span><br><span class="line"> $update( <span class="number">0</span>, <span class="number">1</span> );</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">case</span> <span class="number">2</span> :</span><br><span class="line"> fsockopen( $url, <span class="number">80</span>, $en, $es, <span class="number">4</span> ) ?</span><br><span class="line"> $update( <span class="number">2</span>, <span class="number">2</span> ) : $update( <span class="number">1</span>, <span class="number">2</span> );</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">default</span> :</span><br><span class="line"> $update( <span class="number">0</span>, <span class="number">0</span> );</span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">curl_close( $ch );</span><br></pre></td></tr></table></figure></p><p>能科学上网的服务器代码<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">error_reporting(<span class="number">0</span>); <span class="comment">//这句必须加上, 因为fsockopen打不开的时候会返回warning。这是个api所以要保持数据能正确返回2或者1</span></span><br><span class="line"><span class="keyword">echo</span> fsockopen( base64_decode( $_GET[<span class="string">'url'</span>] ), <span class="number">80</span>, $en, $es, <span class="number">4</span> ) ? <span class="number">2</span> : <span class="number">1</span>;</span><br></pre></td></tr></table></figure></p><p><img src="https://blog-image-1255951271.cos.ap-beijing.myqcloud.com/WX20191217-143917%402x.png" alt="linear-gradient(90deg, red 25%, yellow 37%, red 63%)"></p>]]></content>
</entry>
<entry>
<title>使用redis set集合实现牛牛</title>
<link href="/2018/11/19/%E4%BD%BF%E7%94%A8redis-set%E9%9B%86%E5%90%88%E5%AE%9E%E7%8E%B0%E7%89%9B%E7%89%9B/"/>
<url>/2018/11/19/%E4%BD%BF%E7%94%A8redis-set%E9%9B%86%E5%90%88%E5%AE%9E%E7%8E%B0%E7%89%9B%E7%89%9B/</url>
<content type="html"><![CDATA[<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?php</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">webPuk</span><span class="params">()</span> </span>{</span><br><span class="line">$redis = <span class="keyword">new</span> Redis();</span><br><span class="line">$redis->connect(<span class="string">'127.0.0.1'</span>);</span><br><span class="line"></span><br><span class="line">$randNum = <span class="number">-5</span>; <span class="comment">//随机发5张可能重复的牌</span></span><br><span class="line">$totalNum = <span class="number">5</span>; <span class="comment">//每个玩家几张牌</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//这里c d h s分别代表 黑桃几,红桃几,方块几,梅花几</span></span><br><span class="line">$redis->sadd(<span class="string">'deck'</span>, <span class="string">'CA'</span>, <span class="string">'C2'</span>, <span class="string">'C3'</span>, <span class="string">'C4'</span>, <span class="string">'C5'</span>, <span class="string">'C6'</span>, <span class="string">'C7'</span>, <span class="string">'C8'</span>, <span class="string">'C9'</span>, <span class="string">'C10'</span>, <span class="string">'CJ'</span>, <span class="string">'CQ'</span>, <span class="string">'CK'</span>, <span class="string">'DA'</span>, <span class="string">'D2'</span>, <span class="string">'D3'</span>, <span class="string">'D4'</span>, <span class="string">'D5'</span>, <span class="string">'D6'</span>, <span class="string">'D7'</span>, <span class="string">'D8'</span>, <span class="string">'D9'</span>, <span class="string">'D10'</span>, <span class="string">'DJ'</span>, <span class="string">'DQ'</span>, <span class="string">'DK'</span>, <span class="string">'HA'</span>, <span class="string">'H2'</span>, <span class="string">'H3'</span>, <span class="string">'H4'</span>, <span class="string">'H5'</span>, <span class="string">'H6'</span>, <span class="string">'H7'</span>, <span class="string">'H8'</span>, <span class="string">'H9'</span>, <span class="string">'H10'</span>, <span class="string">'HJ'</span>, <span class="string">'HQ'</span>, <span class="string">'HK'</span>, <span class="string">'SA'</span>, <span class="string">'S2'</span>, <span class="string">'S3'</span>, <span class="string">'S4'</span>, <span class="string">'S5'</span>, <span class="string">'S6'</span>, <span class="string">'S7'</span>, <span class="string">'S8'</span>, <span class="string">'S9'</span>, <span class="string">'S10'</span>, <span class="string">'SJ'</span>, <span class="string">'SQ'</span>, <span class="string">'SK'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//模拟三个三家玩斗牛</span></span><br><span class="line">$result = $redis->SRANDMEMBER(<span class="string">'deck'</span>, $randNum);</span><br><span class="line">$result2 = $redis->SRANDMEMBER(<span class="string">'deck'</span>, $randNum);</span><br><span class="line">$result3 = $redis->SRANDMEMBER(<span class="string">'deck'</span>, $randNum);</span><br><span class="line"></span><br><span class="line"><span class="keyword">echo</span> $msg1 = <span class="string">"玩家1: "</span> . PHP_EOL;</span><br><span class="line"><span class="keyword">foreach</span> ($result <span class="keyword">as</span> $k => $v) {</span><br><span class="line"><span class="keyword">if</span> ($k > $totalNum) {</span><br><span class="line"><span class="keyword">break</span>;</span><br><span class="line">}</span><br><span class="line"> <span class="keyword">echo</span> $v . PHP_EOL;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">echo</span> <span class="string">"<br/>"</span>;</span><br><span class="line"><span class="keyword">echo</span> $msg2 = <span class="string">"玩家2: "</span> . PHP_EOL;</span><br><span class="line"><span class="keyword">foreach</span> ($result2 <span class="keyword">as</span> $k => $v) {</span><br><span class="line"><span class="keyword">if</span> ($k > $totalNum) {</span><br><span class="line"><span class="keyword">break</span>;</span><br><span class="line">}</span><br><span class="line"><span class="keyword">echo</span> $v . PHP_EOL;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">echo</span> <span class="string">"<br/>"</span>;</span><br><span class="line"><span class="keyword">echo</span> $msg3 = <span class="string">"玩家3: "</span> . PHP_EOL;</span><br><span class="line"><span class="keyword">foreach</span> ($result3 <span class="keyword">as</span> $k => $v) {</span><br><span class="line"><span class="keyword">if</span> ($k > $totalNum) {</span><br><span class="line"><span class="keyword">break</span>;</span><br><span class="line">}</span><br><span class="line"><span class="keyword">echo</span> $v . PHP_EOL;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">webPuk();</span><br><span class="line"></span><br><span class="line"><span class="keyword">echo</span> <span class="string">"</br>"</span>;</span><br><span class="line"><span class="keyword">echo</span> <span class="string">"<a href='./run.php'>重新发牌</a>"</span>;</span><br></pre></td></tr></table></figure><p>效果如下:<br>玩家1: H1 D10 HK CQ S2<br>玩家2: DQ D9 S2 D8 C9<br>玩家3: C3 H3 DJ H3 H4 重新发牌</p><p>这里牌的颜色翻译就不写了, 本来打算写完自动算出哪个玩家胜利, </p><p><a href="https://www.cnblogs.com/cnxkey/articles/9175362.html" target="_blank" rel="noopener">参考上篇</a><br><a href="https://www.cnblogs.com/cnxkey/articles/9175368.html" target="_blank" rel="noopener">参考中篇</a><br><a href="https://www.cnblogs.com/cnxkey/articles/9175372.html" target="_blank" rel="noopener">参考下篇</a></p><p>看完以后整个人<br><img src="https://blog-image-1255951271.cos.ap-beijing.myqcloud.com/u%3D2123950941%2C126437839%26fm%3D26%26gp%3D0.jpg" alt=""></p><p><a href="https://www.goxy.top/web-puke.php" target="_blank" rel="noopener">体验</a></p>]]></content>
</entry>
<entry>
<title>php实现异步调用</title>
<link href="/2018/10/20/php%E5%AE%9E%E7%8E%B0%E5%BC%82%E6%AD%A5%E8%B0%83%E7%94%A8/"/>
<url>/2018/10/20/php%E5%AE%9E%E7%8E%B0%E5%BC%82%E6%AD%A5%E8%B0%83%E7%94%A8/</url>
<content type="html"><![CDATA[<p>php默认的请求方式是同步的, 即发送请求必须等到服务端响应才可进行下一步操作。<br>这个默认的机制,导致碰到一些需求会很尴尬. 如:<code>注册时, 掉用了第三方的api服务(人脸识别, 身份证识别啥啥啥的)</code></p><p>这个时候就尴尬了,你的界面必须等待第三方返回方可进行下一步,这对用户的体验是个很大的障碍。 </p><p>这个时候就很适合用异步请求了。只需要调用第三方api无需等待响应。</p><p>下面是个核心的demo。去掉了具体的实现逻辑…</p><p><code>ignore_user_abort(true)</code> <a href="https://www.php.net/manual/zh/function.ignore-user-abort.php" target="_blank" rel="noopener">客户端断开连接,是否中断脚本</a><br><code>set_time_limit(0)</code> <a href="https://www.php.net/manual/zh/function.set-time-limit.php" target="_blank" rel="noopener">脚本执行时间没有限制</a></p><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">ignore_user_abort(<span class="keyword">true</span>); </span><br><span class="line">set_time_limit(<span class="number">0</span>);</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">asynRequest</span><span class="params">($url)</span> </span>{</span><br><span class="line"> </span><br><span class="line"> $fp = fsockopen($url, <span class="number">80</span>, $errno, $errstr, <span class="number">10</span>);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> (<span class="keyword">false</span> === $fp) {</span><br><span class="line"> <span class="keyword">echo</span> <span class="string">"连接失败"</span>;</span><br><span class="line"> <span class="keyword">die</span>;</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> $out = <span class="string">"GET /api.php?age=8&name=getin HTTP/1.1 \r\n"</span>;</span><br><span class="line"> $out .= <span class="string">"Host: "</span>. $url . <span class="string">"\r\n"</span>;</span><br><span class="line"> $out .= <span class="string">"Connection:Close \r\n\r\n"</span>;</span><br><span class="line"> </span><br><span class="line"> fwrite($fp, $out);</span><br><span class="line"> <span class="comment">//echo fread($fp, 1024);</span></span><br><span class="line"> fclose($fp);</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">asynRequest(<span class="string">"www.doman.com"</span>); <span class="comment">//程序执行到这里的时候, 不会等待www.doman.com/api.php的返回结果,而是跳过等待。</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">echo</span> <span class="number">1</span>; <span class="comment">// ?</span></span><br></pre></td></tr></table></figure>]]></content>
</entry>
<entry>
<title>玩玩截取查找字符串</title>
<link href="/2017/12/30/%E7%8E%A9%E7%8E%A9%E6%88%AA%E5%8F%96%E6%9F%A5%E6%89%BE%E5%AD%97%E7%AC%A6%E4%B8%B2/"/>
<url>/2017/12/30/%E7%8E%A9%E7%8E%A9%E6%88%AA%E5%8F%96%E6%9F%A5%E6%89%BE%E5%AD%97%E7%AC%A6%E4%B8%B2/</url>
<content type="html"><![CDATA[<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">inNum</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="keyword">echo</span> <span class="string">"enter money: "</span>;</span><br><span class="line">$num = trim(fgets(STDIN));</span><br><span class="line"></span><br><span class="line">$w = strpos($num, <span class="string">'w'</span>); <span class="comment">//记录下标</span></span><br><span class="line">$k = strpos($num, <span class="string">'k'</span>);</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">if</span> ($w !== <span class="keyword">false</span>) {</span><br><span class="line"></span><br><span class="line"> $fast = mb_substr($num, <span class="number">0</span>, $w);</span><br><span class="line"> $wnum = (int)$fast . <span class="string">'0000'</span> . PHP_EOL;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> ($k !== <span class="keyword">false</span>) { </span><br><span class="line"> $i = $k - $w - <span class="number">1</span>; <span class="comment">//w和k之间的位数</span></span><br><span class="line"> $kn = mb_substr($num, $w+<span class="number">1</span>, $i); </span><br><span class="line"> </span><br><span class="line"> <span class="keyword">if</span> ($kn <= <span class="number">9</span>) {</span><br><span class="line"> $kn = $kn . <span class="string">'000'</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> ($kn <= <span class="number">99</span>) {</span><br><span class="line"> $kn = $kn . <span class="string">'00'</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> ($kn <= <span class="number">999</span>) {</span><br><span class="line"> $kn = $kn. <span class="string">'0'</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> $len = strlen($num) - <span class="number">1</span>;</span><br><span class="line"> $mo = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">if</span> ($len > $k) { <span class="comment">//k后面还有数字</span></span><br><span class="line"> $mo = mb_substr($num, $k+<span class="number">1</span>); <span class="comment">//截取k后面的数字</span></span><br><span class="line"> <span class="keyword">if</span> (strlen($mo) < <span class="number">1</span>) {</span><br><span class="line"> $mo = $mo . <span class="string">'0'</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">echo</span> (int)$wnum + (int)$kn + (int)$mo . PHP_EOL;</span><br><span class="line"> <span class="keyword">die</span>;</span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">echo</span> $wnum;</span><br><span class="line"></span><br><span class="line"> } <span class="keyword">elseif</span> ($k !== <span class="keyword">false</span>) {</span><br><span class="line"></span><br><span class="line"> $fast = mb_substr($num, <span class="number">0</span>, $k);</span><br><span class="line"> <span class="keyword">if</span> ($fast <= <span class="number">9</span>) {</span><br><span class="line"> $fast = $fast . <span class="string">'000'</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> ($fast <= <span class="number">99</span>) {</span><br><span class="line"> $fast = $fast . <span class="string">'00'</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> ($fast <= <span class="number">999</span>) {</span><br><span class="line"> $fast = $fast . <span class="string">'0'</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">echo</span> $fast . PHP_EOL;</span><br><span class="line"></span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"></span><br><span class="line"> <span class="keyword">echo</span> $num . PHP_EOL; </span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">inNum();</span><br></pre></td></tr></table></figure><h3 id="效果图"><a href="#效果图" class="headerlink" title="效果图"></a>效果图</h3><p><img src="https://sign-super.oss-cn-hangzhou.aliyuncs.com/shudun/num.png" alt="linear-gradient(90deg, red 25%, yellow 37%, red 63%)"></p>]]></content>
</entry>
<entry>
<title>php实现冒泡排序</title>
<link href="/2017/05/17/%E5%86%92%E6%B3%A1%E6%8E%92%E5%BA%8F/"/>
<url>/2017/05/17/%E5%86%92%E6%B3%A1%E6%8E%92%E5%BA%8F/</url>
<content type="html"><![CDATA[<p>冒泡排序的概念, 就是相邻的元素进行比较, 条件成立交换位置</p><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?php</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">simpleSort</span><span class="params">(array $data)</span> </span>{</span><br><span class="line"><span class="comment">//从第一个元素开始比较</span></span><br><span class="line"><span class="keyword">for</span> ($i = <span class="number">0</span>, $count = count($data); $i < $count; $i++) { </span><br><span class="line"><span class="comment">//跟相邻的元素比较</span></span><br><span class="line"><span class="keyword">for</span> ($n = $i + <span class="number">1</span>; $n < $count; $n++) {</span><br><span class="line"><span class="keyword">if</span> ($data[$i] < $data[$n]) {</span><br><span class="line"><span class="comment">//满足条件就交换位置</span></span><br><span class="line"><span class="keyword">list</span>($data[$i], $data[$n]) = [$data[$n], $data[$i]];</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> $data;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">print_r(simpleSort([<span class="number">1</span>,<span class="number">12</span>,<span class="number">6</span>,<span class="number">33</span>,<span class="number">7</span>,<span class="number">47</span>,<span class="number">66</span>,<span class="number">84</span>,<span class="number">51</span>]));</span><br><span class="line"><span class="meta">?></span></span><br></pre></td></tr></table></figure><p>运行结果:<br><code>Array( [0] => 84 [1] => 66 [2] => 51 [3] => 47 [4] => 33 [5] => 12 [6] => 7 [7] => 6 [8] => 1)</code></p>]]></content>
</entry>
<entry>
<title>微测foreach和array_map的执行速度</title>
<link href="/2017/01/17/%E5%BE%AE%E6%B5%8Bforeach%E5%92%8Carray-map%E7%9A%84%E6%89%A7%E8%A1%8C%E9%80%9F%E5%BA%A6/"/>
<url>/2017/01/17/%E5%BE%AE%E6%B5%8Bforeach%E5%92%8Carray-map%E7%9A%84%E6%89%A7%E8%A1%8C%E9%80%9F%E5%BA%A6/</url>
<content type="html"><![CDATA[<p><code>foreach</code> 和 <code>array_map</code> 谁的执行速度比较快? 今天闲的蛋疼, 写了个小demo test</p><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?php</span></span><br><span class="line">$data = [</span><br><span class="line">[<span class="string">'name'</span> => <span class="string">'salina'</span>, <span class="string">'age'</span> => <span class="number">16</span>, <span class="string">'sex'</span> => <span class="number">0</span>, <span class="string">'status'</span> => <span class="number">0</span>],</span><br><span class="line">];</span><br><span class="line"></span><br><span class="line">ini_set(<span class="string">'memory_limit'</span>, <span class="string">'-1'</span>);</span><br><span class="line"><span class="keyword">for</span> ($i = <span class="number">0</span>; $i < <span class="number">9999999</span>; $i++) {</span><br><span class="line">array_push($data, [<span class="string">'name'</span> => <span class="string">'liya'</span> . $i, <span class="string">'age'</span> => <span class="number">22</span>, <span class="string">'sex'</span> => <span class="number">0</span>, <span class="string">'status'</span> =><span class="number">1</span>]);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">$begin = microtime(<span class="keyword">true</span>);</span><br><span class="line">$data = array_map(<span class="function"><span class="keyword">function</span><span class="params">($v)</span> </span>{</span><br><span class="line"><span class="keyword">if</span> ($v[<span class="string">'status'</span>] === <span class="number">1</span>) { <span class="comment">//一些简单的转换</span></span><br><span class="line">$v[<span class="string">'status'</span>] = <span class="string">'激活'</span>;</span><br><span class="line">} <span class="keyword">else</span> {</span><br><span class="line">$v[<span class="string">'status'</span>] = <span class="string">'禁用'</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> ($v[<span class="string">'sex'</span>] === <span class="number">1</span>) {</span><br><span class="line">$v[<span class="string">'sex'</span>] = <span class="string">'男'</span>;</span><br><span class="line">} <span class="keyword">else</span> {</span><br><span class="line">$v[<span class="string">'sex'</span>] = <span class="string">'女'</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> $v;</span><br><span class="line">}, $data);</span><br><span class="line">$end = microtime(<span class="keyword">true</span>);</span><br><span class="line"></span><br><span class="line">$min = microtime(<span class="keyword">true</span>);</span><br><span class="line"><span class="keyword">foreach</span> ($data <span class="keyword">as</span> $k => &$v) {</span><br><span class="line"><span class="keyword">if</span> ($v[<span class="string">'status'</span>] === <span class="number">1</span>) {</span><br><span class="line">$v[<span class="string">'status'</span>] = <span class="string">'激活'</span>;</span><br><span class="line">} <span class="keyword">else</span> {</span><br><span class="line">$v[<span class="string">'status'</span>] = <span class="string">'禁用'</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> ($v[<span class="string">'sex'</span>] === <span class="number">1</span>) {</span><br><span class="line">$v[<span class="string">'sex'</span>] = <span class="string">'男'</span>;</span><br><span class="line">} <span class="keyword">else</span> {</span><br><span class="line">$v[<span class="string">'sex'</span>] = <span class="string">'女'</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line">$max = microtime(<span class="keyword">true</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">echo</span> <span class="string">'array_map time: '</span> . ($end - $begin) . PHP_EOL;</span><br><span class="line"><span class="keyword">echo</span> <span class="string">'foreach time: '</span> . ($max - $min) . PHP_EOL;</span><br><span class="line"></span><br><span class="line"><span class="meta">?></span></span><br></pre></td></tr></table></figure><p>执行几次测试结果:<br><code>array_map time: 5.3077862262726</code><br><code>foreach time: 2.8736588954926</code><br>——————————————————-华丽的分割线<br><code>array_map time: 8.4756429195404</code><br><code>foreach time: 12.029865980148</code><br>——————————————————-华丽的分割线2<br><code>array_map time: 5.2941391468048</code><br><code>foreach time: 2.8984580039978</code><br>——————————————————-华丽的分割线3<br><code>array_map time: 5.5151488780975</code><br><code>foreach time: 7.3730530738831</code><br>——————————————————-华丽的分割线4<br><code>array_map time: 5.3060140609741</code><br><code>foreach time: 2.8992168903351</code><br>——————————————————-华丽的分割线5<br><code>array_map time: 5.3467030525208</code><br><code>foreach time: 5.5229871273041</code><br>——————————————————-华丽的分割线6</p><p>结局并不是很理想…</p>]]></content>
</entry>
<entry>
<title>闲聊打怪升级之路</title>
<link href="/2016/12/31/%E9%97%B2%E8%81%8A%E6%89%93%E6%80%AA%E5%8D%87%E7%BA%A7%E4%B9%8B%E8%B7%AF/"/>
<url>/2016/12/31/%E9%97%B2%E8%81%8A%E6%89%93%E6%80%AA%E5%8D%87%E7%BA%A7%E4%B9%8B%E8%B7%AF/</url>
<content type="html"><![CDATA[<p>比如翻文档的时候, 你遇到了这个数组array_walk?what ?这是干啥的。 然后本着不弄明白不回头的心情点开进去看了下介绍如下<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">array_walk ( <span class="keyword">array</span> &$array , callable $callback [, mixed $userdata = <span class="keyword">NULL</span> ] ) : bool</span><br><span class="line">将用户自定义函数 funcname 应用到 <span class="keyword">array</span> 数组中的每个单元。</span><br><span class="line"></span><br><span class="line">array_walk() 不会受到 <span class="keyword">array</span> 内部数组指针的影响。array_walk() 会遍历整个数组而不管指针的位置。</span><br></pre></td></tr></table></figure><br>哦 callback? callback是啥, 于是乎跳转链接google回调函数->搜到了匿名函数, 匿名函数是啥? 跳回了手册 搜索匿名函数。<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">匿名函数(Anonymous functions,也叫闭包函数(closures),允许 临时创建一个没有指定名称的函数。最经常用作回调函数(callback)参数的值。当然,也有其它应用的情况。</span><br><span class="line"></span><br><span class="line">匿名函数目前是通过 Closure 类来实现的。</span><br></pre></td></tr></table></figure><br>哦,闭包函数? 闭包函数是啥, 于是乎继续往下翻, 这时候忽然又发现了个叫协程的好玩意。 听起来就有兴趣学习</p><p>最后…协程是什么, 怎么怎么看着看着又推荐我swoole。 </p><p>swoole是啥? 打开一看 哦。。swoole是个用c写的php的扩展。。。</p><p>于是git clone swoole.git到本地..</p><p><img src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2892676305,1854844533&fm=26&gp=0.jpg" alt="linear-gradient(50deg, red 25%, yellow 37%, red 63%)"></p>]]></content>
</entry>
<entry>
<title>mysql远程登录</title>
<link href="/2016/12/20/mysql%E8%BF%9C%E7%A8%8B%E7%99%BB%E5%BD%95/"/>
<url>/2016/12/20/mysql%E8%BF%9C%E7%A8%8B%E7%99%BB%E5%BD%95/</url>
<content type="html"><![CDATA[<p>如果设置了mysql的host为%,并且在/etc/my.conf下吧bind=address=120.0.0.1注释了<br>还是登录不了的话 可能是服务器运营商的安全组设置问题</p><p>其实整体思路就是用测试工具连接看是否能连接</p><p>我这里用了nc ip port<br>比如<br><code>php$ nc 44.56.34.33 3306</code></p><p>如果提示连接失败就是以上的原因了</p><p><img src="https://sign-super.oss-cn-hangzhou.aliyuncs.com/shudun/shudun.jpg" alt="linear-gradient(10deg, red 25%, yellow 37%, red 63%)"></p>]]></content>
</entry>
</search>