How to be a better PHP Developer

How to be a better PHP Developer

WebConf Taiwan 2013 議程簡報

F830ec52d5bf72ee64fd1a43a6a82a49?s=128

大澤木小鐵

January 12, 2013
Tweet

Transcript

  1. 3.

  2. 4.

  3. 5.

  4. 11.

  5. 15.

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

    list( , $el) = getElements(); // PHP 5.4 :⽤用⽅方括號來直接取⽤用元素 $el = getElements()[1];
  6. 16.

    瞭解原生函式 // 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);
  7. 31.

  8. 36.

    Any fool can write code that a computer can understand.

    Good programmers write code that humans can understand. Martin Fowler
  9. 37.

    以抽象角度看事物 // 找出特定作者的所有⽂文章 $sql = 'SELECT * FROM articles WHERE

    '; $sql .= 'author = :author'; $sth = $dbh->prepare($sql); $sth->execute(array( ':author' => 'jaceju' )); $articles = $sth->fetchAll();
  10. 42.
  11. 44.

  12. 52.

    不做重複的事 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 秒
  13. 53.

    不做重複的事 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 秒