Skip to main content

Tedshd's Dev note

Category: PHP

php - Ubuntu 14 PHP 5.6 or to upgrade from PHP 5.5

# php - Ubuntu 14 PHP 5.6 or to upgrade from PHP 5.5 sudo apt-get -y update sudo add-apt-repository ppa:ondrej/php5-5.6 sudo apt-get -y update sudo apt-get -y install php5 php5-mhash php5-mcrypt php5-curl php5-cli php5-mysql php5-gd php5-intl php5-xsl php5-bcmath if use php -v check version is 5.5.x I use sudo apt-get -u install php5-cli Refer - PHP 5.5 or 5.6—Ubuntu ...

PHP - Auto update static files cache

# PHP - Auto update static files cache We can cache static files(CSS, JavaScript) to client When Server update static code. We want to update cache then we can modify file or add query string update version. We have some choice like modify url ?v=1 to ?v=2 But I want to auto modify version. This is a way i use <?php function autoversion($url) { $ver = stat($_SERVER['DOCUMENT_ROOT'] . $url)[mtime]; return $url . ...

PHP - install phpcs(PHP CodeSniffer)

# PHP - install phpcs(PHP CodeSniffer) Use pear install phpcs ## in Linux(Ubuntu 14.04) sudo pear install --alldeps php_codesniffer ## in Mac(Mac OS X 10.10.1 Yosemite) ### install pear sudo curl -O http://pear.php.net/go-pear.phar then sudo php -d detect_unicode=0 go-pear.phar Update pear upgrade pear pear upgrade ### install codesniffer sudo pear install --alldeps php_codesniffer check phpcs -i return The installed coding standards are MySource, PEAR, PHPCS, PSR1, PSR2, Squiz and Zend if return ...

Detect mobile device

# Detect mobile device Use PHP or JavaScript detect mobile device like iPhone, iPad, iPod or Android… ## Android ### PHP <?php $ua = strtolower($_SERVER['HTTP_USER_AGENT']); if(stripos($ua, 'android') !== false) { echo 'Android!!'; exit; } ?> ### JavaScript var ua = navigator.userAgent.toLowerCase(); if(ua.indexOf("android") > -1) { document.write('Android!!'); } Refer - Android Detection with JavaScript or PHP ## Apple mobile device Refer - How to detect iPhone, iPod and iPad with PHP Refer - How to Identify an Apple iPhone, iPod or iPad Visitor to Your Website ...

PHP Note

# PHP Note ## Notice 處理 db 的部分放到 model 處理來增強可用性 減少邏輯判斷 以錯誤為優先處裡 ## array to string <?php $arr = array( 'val' => 1, 'str' => 'string', ); $to_string = serialize($arr); echo $to_string; // a:2:{s:3:"val";i:1;s:3:"str";s:6:"string";} $to_array = unserialize($to_string); var_dump($to_array); /* not better * if $to_string can't unserialize, it has PHP error */ ?> ## coding style 在 PHP 中有許多邏輯判斷 當有許多條件要去檢查時, 一般來說就是用 if 一直寫下去 最後就造成下面的情況 if ($a === $b) { if ($c) { if ($c >= 10) { if ($d) { // do something // . ...