php


Sep. 2, 2017

Poker StraightFlush

First of all, PHP Code Example. (PHP ≥ 7) function isStriaghtFlush($d) { $b = $f = 0; $m = ['a'=>1,1=>10,'j'=>11,'q'=>12,'k'=>13]; foreach ($d as $a) { $p = substr($a,-1); $b |= 1<<(($m[$a[0]])??$a[0]); $f = ($f===0||$f===$p) ? $p : 1; } $b = ($b << 3) | ($b >> 10); foreach (range(0,13) as $i) { if ((($b >> $i) & 31) == 31){ return [true, $f!=1]; } } return [false, false]; } Explain: A can be straight of A, 2, 3, 4, 5 and 10, J, Q, K A There are total 14 ranks, when getting rank n from the $d, we set 1 of the n element in bitwise map. If we could find any 5 continuously 1 in the bitwise map, then it’s straight. also if all faces are same, then it’s straight flush. Example, cards: 10, J, Q, K, A read 10, bitwise map: 0,0,0,1,0,0,0,0,0,0,0,0,0,0 read J, bitwise map: 0,0,1,1,0,0,0,0,0,0,0,0,0,0 read Q, bitwise map: 0,1,1,1,0,0,0,0,0,0,0,0,0,0 read K, bitwise map: 1,1,1,1,0,0,0,0,0,0,0,0,0,0 read A, bitwise map: 1,1,1,1,0,0,0,0,0,0,0,0,1,0 shift left 3 of bitwise map to 1,1,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0 shift right 10 of bitwise map to 1,1,1,1 OR this two bitwise maps and get a new map 1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1 then we have 5 continuously 1, it’s straight.

Jan. 15, 2015

Install Mcrypt on Mac by Homebrew

Recently I setup Laravel enviroment on my Macbook, when I create project or run php artisan, it always prompts Mcrypt PHP extension required. error for me. Becuase I am using vagrant homestead for Laravel development, I can homestead ssh to run commands, but I think it’s better and convenience for me to install Mcrypt on my local enviroment. Then I did some reseach, and setup Mcrypt with mac native PHP. There are three ways I found: Manually complie Mcrypt source and install Using Homebrew to install Using MAMP Mcrypt For my requirement, I think homebrew is the best way for me. First Install autoconf which is needed when homebrew compling Mcrypt brew install autoconf Homebrew tap Homebrew dupes, versions, and homebrew-php tap brew tap homebrew/dupes brew tap homebrew/versions brew tap homebrew/homebrew-php Install php55 or php56 brew install php55 or brew install php56 PHP Mcrypt brew install php55-mcrypt or brew install php56-mcrypt Last add mcrypt extension into /etc/php.ini. You can find path using brew info php55-mcrypt, eg. extension= /usr/local/Cellar/php55-mcrypt/5.5.20/mcrypt.so

Jul. 15, 2013

JSONP and JSON

Let’s see what’s differences between JSON and JSONP first; //JSON {"name":"stackoverflow","id":5} //JSONP func({"name":"stackoverflow","id":5}); JSONP is the result that you can load the json as a script file. And it is usually used to allow for cross-site AJAX with JSON data. function func(json){ alert(json.name); } var elm = document.createElement("script"); elm.setAttribute("type", "text/javascript"); elm.src = "http://example.com/jsonp"; document.body.appendChild(elm); Convert JSON to JSONP in PHP, JSONP will pass callback to script $json = json_encode($data); $jsonp_callback = isset($_GET['callback']) ? $_GET['callback'] : null; echo $jsonp_callback ? "$jsonp_callback($json)" : $json; Rerference: Stack Overflow

Jun. 25, 2013

Example of $this and self:: in PHP

Saw this good example from stackoverflow class Person { private $name; public function __construct($name) { $this->name = $name; } public function getName() { return $this->name; } public function getTitle() { return $this->getName()." the person"; } public function sayHello() { echo "Hello, I'm ".$this->getTitle()."<br/>"; } public function sayGoodbye() { echo "Goodbye from ".self::getTitle()."<br/>"; } } class Geek extends Person { public function __construct($name) { parent::__construct($name); } public function getTitle() { return $this->getName()." the geek"; } } $geekObj = new Geek("Ludwig"); $geekObj->sayHello(); $geekObj->sayGoodbye(); Output: Hello, I'm Ludwig the geek Goodbye from Ludwig the person $this will refer to current object(extended class), but self:: only refer to current class. If you want to refer to current object, you can use static:: (>= PHP5.3.0)