Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How to be a better PHP Developer

How to be a better PHP Developer

WebConf Taiwan 2013 議程簡報

大澤木小鐵

January 12, 2013
Tweet

More Decks by 大澤木小鐵

Other Decks in Programming

Transcript

  1. 瞭解語法 // 交換變數內容 list($a, $b) = array($b, $a); // 直接取得指定位置的值

    list( , $el) = getElements(); // PHP 5.4 :⽤用⽅方括號來直接取⽤用元素 $el = getElements()[1];
  2. 瞭解原生函式 // Bad $diff_ids = array(); foreach ($ids as $id)

    { if (!in_array($id, $actual_ids)) { $diff_ids[] = $id; } } // Good $diff_ids = array_diff($ids, $actual_ids);
  3. Any fool can write code that a computer can understand.

    Good programmers write code that humans can understand. Martin Fowler
  4. 以抽象角度看事物 // 找出特定作者的所有⽂文章 $sql = 'SELECT * FROM articles WHERE

    '; $sql .= 'author = :author'; $sth = $dbh->prepare($sql); $sth->execute(array( ':author' => 'jaceju' )); $articles = $sth->fetchAll();
  5. 不做重複的事 function fib($n) { if ($n < 2) return $n;

    $f[$n] = fib($n - 2) + fib($n - 1); return $f[$n]; } echo fib(30), "\n"; // 結果: 832040 ,共執⾏行 5.59556889534 秒
  6. 不做重複的事 function fib($n) { static $f = array(); if (isset($f[$n]))

    return $f[$n]; if ($n < 2) return $n; $f[$n] = fib($n - 2) + fib($n - 1); return $f[$n]; } echo fib(30), "\n"; // 結果: 832040 ,共執⾏行 0.002144813537 秒