PHP中的null合并运算符

null合并运算符是一个好东西,有了它我们就能很方便的获取一个参数,并能在其为空的情况下提供一个默认值。比如在js中可以用||来搞:
function setSomething(a){a = a || 'some-default-value';// ...}
而在PHP中,可惜PHP的||总是返回truefalse,无法这样来搞。PHP7才正式加入了??这个运算符:
// 获取user参数的值(如果为空,则用'nobody')$username = $_GET['user'] ?? 'nobody';// 等价于:$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
PHP7 估计还有很长时间才能用到生产环境中,那在目前的PHP5中有没有替代方案呢?据研究,完全有个非常便捷的替代方案:
// 获取user参数的值(如果为空,则用'nobody')$username = @$_GET['user'] ?: 'nobody';// 等价于:$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
-- 运行此代码:https://3v4l.org/aDUW8瞪大了眼睛看,跟前面的PHP7的例子差不多,主要是把??替换为了?:。 这个是个什么鬼呢?其实这就是(expr1) ? (expr2) : (expr3)表达式的省略模式:
表达式 (expr1) ? (expr...
阅读全文

PlantUML的实例参考

Alice -> Bob: Authentication RequestBob --> Alice: Authentication ResponseAlice -> Bob: Another authentication RequestAlice <-- Bob: another authentication Response

actor Foo1boundary Foo2control Foo3entity Foo4database Foo5Foo1 -> Foo2 : To boundaryFoo1 -> Foo3 : To controlFoo1 -> Foo4 : To entityFoo1 -> Foo5 : To database

actor Bob #red' The only difference between actor'and participant is the drawingparticipant Aliceparticipant "I have a really\nlong name" as L #99FF99/' You can also declare:participant L as "I have a really\nlong name"  #99FF99'/Alice->Bob: Authentication RequestBob->Alice: Authentication ResponseBob->L: Log transaction

Alice -> "Bob()" : Hello"Bob()" -> "This is very\nlong" as Long' You can also declare:' "Bob()" -> Long as "This is very\nlong"Long --> "Bob()" : ok

Alice->Alice: This is a signal to self.\nIt also demonstrates\nmultiline \ntext

Bob ->x AliceBob -> AliceBob ->> AliceBob -\ AliceBob \\- AliceBob //-- AliceBob ->o AliceBob o\\-- AliceBob <-> AliceBob <->o Alice

Bob -[#red]> Alice : helloAlice -[#0000FF]->Bob : okautonumberBob -> Alice : Authentication RequestBob <- Alice : Authentication Responseautonumber 15Bob -> Alice : Another authentication RequestBob <- Alice : Another authentication Responseautonumber 40 10Bob -> Alice : Yet another authentication RequestBob <- Alice : Yet another authentication Responseautonumber "<b>[000]"Bob -> Alice : Authentication RequestBob <- Alice : Authentication Responseautonumber 15 "<b>(<u>##</u>)"Bob -> Alice : Another authentication RequestBob <- Alice : Another authentication Responseautonumber 40 10 "<font color=red><b>Message 0  "Bob -> Alice : Yet another authentication RequestBob <- Alice : Yet another authentication Response

title Simple communication exampleAlice -> Bob: Authentication RequestBob --> Alice: Authentication ResponseAlice -> Bob : Hellolegend rightShortlegendendlegend

Alice -> Bob : message 1Alice -> Bob : message 2newpageAlice -> Bob : message 3Alice -> Bob : message 4newpage A title for the\nlast pageAlice -> Bob : message 5Alice -> Bob : message 6

Alice -> Bob: Authentication Requestalt successful caseBob -> Alice: Authentication Acceptedelse some kind of failureBob -> Alice: Authentication Failuregroup My own labelAlice -> Log : Log attack startloop 1000 timesAlice -> Bob: DNS AttackendAlice -> Log : Log attack endendelse Another type of failureBob -> Alice: Please repeatend

Alice->Bob : hellonote left: this is a first noteBob->Alice : oknote right: this is another noteBob->Bob : I am thinkingnote lefta notecan also be definedon several linesend note

participant Aliceparticipant Bobnote left of Alice #aquaThis is displayedleft of Alice.end notenote right of Alice: This is displayed right of Alice.note over Alice: This is displayed over Alice.note over Alice, Bob #FFAAAA: This is displayed\n over Bob and Alice.note over Bob, AliceThis is yet anotherexample ofa long note.end note

caller -> server : conReqhnote over caller : idlecaller <- server : conConfrnote over server"r" as rectangle"h" as hexagonendrnote

participant Aliceparticipant "The **Famous** Bob" as BobAlice -> Bob : hello --there--... Some ~~long delay~~ ...Bob -> Alice : oknote leftThis is **bold**This is //italics//This is ""monospaced""This is --stroked--This is __underlined__This is ~~waved~~end noteAlice -> Bob : A //well formatted// messagenote right of AliceThis is <back:cadetblue><size:18>displayed</size></back>__left of__ Alice.end notenote left of Bob<u:red>This</u> is <color #118888>displayed</color>**<color purple>left of</color> <s:red>Alice</strike> Bob**.end notenote over Alice, Bob<w:#FF33FF>This is hosted</w> by <img sourceforge.jpg>end note

== Initialization ==Alice -> Bob: Authentication RequestBob --> Alice: Authentication Response== Repetition ==Alice -> Bob: Another authentication RequestAlice <-- Bob: another authentication Response

participant Aliceactor Bobref over Alice, Bob : initAlice -> Bob : helloref over BobThis can be onseveral linesend ref

Alice -> Bob: Authentication Request...Bob --> Alice: Authentication Response...5 minutes latter...Bob --> Alice: Bye !

Alice -> Bob: message 1Bob --> Alice: ok|||Alice -> Bob: message 2Bob --> Alice: ok||45||Alice -> Bob: message 3Bob --> Alice: ok

participant UserUser -> A: DoWorkactivate AA -> B: << createRequest >>activate BB -> C: DoWorkactivate CC --> B: WorkDonedestroy CB --> A: RequestCreateddeactivate BA -> User: Donedeactivate A

participant UserUser -> A: DoWorkactivate A #FFBBBBA -> A: Internal callactivate A #DarkSalmonA -> B: << createRequest >>activate BB --> A: RequestCreateddeactivate Bdeactivate AA -> User: Donedeactivate A

Bob -> Alice : hellocreate OtherAlice -> Other : newcreate control StringAlice -> Stringnote right : You can also put notes!Alice --> Bob : ok

[-> A: DoWorkactivate AA -> A: Internal callactivate AA ->] : << createRequest >>A<--] : RequestCreateddeactivate A[<- A: Donedeactivate A

[-> Bob[o-> Bob[o->o Bob[x-> Bob[<- Bob[x<- BobBob ->]Bob ->o]Bob o->o]Bob ->x]Bob <-]Bob x<-]

@startuml/'This is an example Sequence diagramShowing a fictional web feature flowIt is easy to see roles & responsibilities for each componentand easy to change them too...'/title "Story Feature Example - Sequence Diagram"'This is a single line comment/'This is a multi-line commentOne another line'/actor User'boundary Proxyparticipant UIServer as UIparticipant APIUser -> UI: LINK: goto /account pageactivate UIUI -> User:deactivate UIactivate Usernote over UserRequirments:UI: Setting PageClick delete buttonend note' user requests account/delete pageUser -> UI: /account/deletedeactivate Useractivate UIUI -> User:deactivate UI' confirmation formactivate Usernote over UserUI: FormAre you sure?end noteUser -> UI: POST /account/deletedeactivate Useractivate UInote over UI: Verify: user has confirmedUI -> API: /account/deleteactivate APIAPI -> API: deleteAPI -> UI: donedeactivate APIUI -> User: successdeactivate UI@enduml

阅读全文

使用PHPUnit + Selenium进行自动化测试

第一步,安装PHPUni

最简单的方式莫过于到PHPUnit的官网下载PHPUnit,然后把那个下载了的phpunit.phar加到PATH中。此外,还可以通过pear来安装phpunit:
pear clear-cachepear channel-discover pear.phpunit.depear channel-discover components.ez.nopear channel-discover pear.symfony-project.compear install -a -f  phpunit/PHPUnit
参考:http://www.cnblogs.com/wanfox/p/4613517.html然而使用pear安装方式,我却是没有成功!大概是因为天朝的神奇的GFW吧!补记:还能通过composer来安装:
composer require phpunit/phpunit

第二步,下载Selenium Standalone Serve

传送门http://docs.seleniumhq.org/download/传送门2:https://selenium-release.storage.googleapis.com/index.html?path=2.48/下载下来一般是一个selenium-server-standalone-2.48.2.jar这样的文件(版本号可能会有所更新)。

第三步,下载WebDriver for Chrom...

阅读全文

关于clarence-pan.gitcafe.io

由于github.io在国内访问的会比较慢,并且时常会出现无法连接的现象(原因你懂的),特设立了clarence-pan.gitcafe.io作为本博客的镜像站点。欢迎访问。若问如何架设gitcafe上的博客?请移步这里观看官方文档,很简单的。
后记:gitcafe已经挂了,请访问clarencep.com.阅读全文

一个短信中最多可以容纳多少中英文混合的字符?

今天遇到一个问题——一个短信里面最多可以容纳多少个中英文混编的字符?顺手google了以下,发现这个事情跟我的直觉还是不太一样的,特此记录,以备后查。直觉是
  1. 一条短信最多70个中文字符...
阅读全文

如何在静态博客中嵌入评论

背景

静态博客(如本博客使用的是hexo来生成的),有很多优点,比如能抗很大的流量,而且有http://github.io这样的免费的托管可以使用。但是,毕竟只是html,于是一般只能浏览,没法与访问者进行互动,访问者没法评论。这那能行呢?这两天在访问mindhacks上的一篇文章的时候,发现这位博主使用的多说挺有意思的,能提供对这种静态博客的评论。于是乎,闲暇之际在偶自己的静态博客试用下,看看如何...

偶的多说试用小记

首先,登录多说

进入多说的官网,然后有一个【我要安装】,点击后会提示登录。这时可以有好多种open API登录方式可以选择,偶就随便选个QQ登录,然后登录上去了。

接着,获取评论框代码

多说的API还是很简洁的,除了公共js代码外,评论框就一行:
<div class="ds-thread" data-thread-key="请将此处替换成文章在你的站点中的ID" data-title="请替换成文章的标题" data-url="请替换成文章的网址"></div>

然后,嵌入到博客中

本博客是he...阅读全文

Ubuntu下配置DNS服务器

一、安装DNS服务器-bind

bind9可以直接用apt-get安装
sudo apt-get install bind9
#二、配置服务器bind9的配置文件在/etc/bind/目录下,入口文件是named.conf,这个文件包含了named.conf.options,named.conf.localnamed.conf.default-zones。其中named.conf.options中包含了以下内容:
options {// 缓存目录directory "/var/cache/bind";// 本机解析不了的DNS该转发到哪里forwarders {8.8.8.8;};dnssec-validation auto;auth-nxdomain no;    # conform to RFC1035listen-on-v6 { any; };};
named.conf.local一般是空的,不用管它。named.conf.default-zones中包含了解析的域名的配置,节选如下:
// 正向解析配置,即从域名localhost解析到127.0.0.1zone "localhost" {type master;file "/etc/bind/db.local";};// 反向解析配置,即从IP 127.0.0.1解析到localhostzone "127.in-addr.arpa" {type master;file "/etc/bind/db.127";};
具体的配置都在文件db.xxxx中,比如db.local中如下:
;; BIND data file for local loopback interface;$TTL    604800; 下面的root.localhost.是管理员邮箱,其中的'@'被替换为了'.'@       IN      SOA     localhost. root.localhost. (2         ; Serial 即序列号,或者说版本号,每次改动后应修改下这里604800         ; Refresh 刷新时间(单位秒,下同)86400         ; Retry 重试时间2419200         ; Expire 过期时间604800 )       ; Negative Cache TTL 缓存TTL;@       IN      NS      localhost.      ; 指定域名@       IN      A       127.0.0.1       ; A记录,即指向IPv4地址的记录@       IN      AAAA    ::1             ; 指向IPv6地址的记录

三、 如何增加一条DNS记录

最简单的办法,copy+paste,然后改吧改吧试试,比如增...阅读全文