เริ่มต้นเขียนโปรแกรมง่ายๆ กับ QBASIC สำหรับคนไม่เป็นมาก่อน

เป็น slide สอนน้องๆ ปี 1 ตอนเป็น TA ช่วยอาจารย์สอนอยู่ที่มหาลัย (เมื่อเกือบๆ 7 ปีก่อน) ใครเรียนเกี่ยวกับคอมพิวเตอร์ปี 2-4 แล้วอ่านไม่เข้าใจพิจารณาตัวเองได้แล้วนะครับ เพราะ slide นี้สอนเด็กคณะแพทย์, สหเวช, เภสัช, เกษตร ฯลฯ อีกเยอะที่ไม่เกี่ยวกับคอมแล้วเค้าทำ Lab ภาคปฏิบัติและทำข้อสอบได้มาแล้ว (อย่าอ้างว่าเขียนไม่ได้ อาจารย์ไม่สอนนะเด็กภาคคอมฯ)

50+ PHP optimisation !

เอามาจาก 50+ PHP optimisation tips revisited ขอโน็ตไว้แล้วกัน กลัวหาย

  1. echo is faster than print. [Citation]
  2. Wrap your string in single quotes (’) instead of double quotes (”) is faster because PHP searches for variables inside “…” and not in ‘…’, use this when you’re not using variables you need evaluating in your string. [Citation]
  3. Use sprintf instead of variables contained in double quotes, it’s about 10x faster. [Citation]
  4. Use echo’s multiple parameters (or stacked) instead of string concatenation. [Citation]
  5. Use pre-calculations, set the maximum value for your for-loops before and not in the loop. ie: for ($x=0; $x < count($array); $x), this calls the count() function each time, use $max=count($array) instead before the for-loop starts. [Citation]
  6. Unset or null your variables to free memory, especially large arrays. [Citation]
  7. Avoid magic like __get, __set, __autoload. [Citation]
  8. Use require() instead of require_once() where possible. [Citation]
  9. Use full paths in includes and requires, less time spent on resolving the OS paths. [Citation]
  10. require() and include() are identical in every way except require halts if the file is missing. Performance wise there is very little difference. [Citation]
  11. Since PHP5, the time of when the script started executing can be found in $_SERVER[’REQUEST_TIME’], use this instead of time() or microtime(). [Citation]
  12. PCRE regex is quicker than EREG, but always see if you can use quicker native functions such as strncasecmp, strpbrk and stripos instead. [Citation]
  13. When parsing with XML in PHP try xml2array, which makes use of the PHP XML functions, for HTML you can try PHP’s DOM document or DOM XML in PHP4. [Citation]
  14. str_replace is faster than preg_replace, str_replace is best overall, however strtr is sometimes quicker with larger strings. Using array() inside str_replace is usually quicker than multiple str_replace. [Citation]
  15. “else if” statements are faster than select statements aka case/switch. [Citation]
  16. Error suppression with @ is very slow. [Citation]
  17. To reduce bandwidth usage turn on mod_deflate in Apache v2 [Citation] or for Apache v1 try mod_gzip. [Citation]
  18. Close your database connections when you’re done with them. [Citation]
  19. $row[’id’] is 7 times faster than $row[id], because if you don’t supply quotes it has to guess which index you meant, assuming you didn’t mean a constant. [Citation]
  20. Use <?php … ?> tags when declaring PHP as all other styles are depreciated, including short tags. [Citation]
  21. Use strict code, avoid suppressing errors, notices and warnings thus resulting in cleaner code and less overheads. Consider having error_reporting(E_ALL) always on. [Citation]
  22. PHP scripts are be served at 2-10 times slower by Apache httpd than a static page. Try to use static pages instead of server side scripts. [Citation]
  23. PHP scripts (unless cached) are compiled on the fly every time you call them. Install a PHP caching product (such as memcached or eAccelerator or Turck MMCache) to typically increase performance by 25-100% by removing compile times. You can even setup eAccelerator on cPanel using EasyApache3. [Citation]
  24. An alternative caching technique when you have pages that don’t change too frequently is to cache the HTML output of your PHP pages. Try Smarty or Cache Lite. [Citation]
  25. Use isset where possible in replace of strlen. (ie: if (strlen($foo) < 5) { echo “Foo is too short”; } vs. if (!isset($foo{5})) { echo “Foo is too short”; } ). [Citation]
  26. ++$i is faster than $ i++, so use pre-increment where possible. [Citation]
  27. Make use of the countless predefined functions of PHP, don’t attempt to build your own as the native ones will be far quicker; if you have very time and resource consuming functions, consider writing them as C extensions or modules. [Citation]
  28. Profile your code. A profiler shows you, which parts of your code consumes how many time. The Xdebug debugger already contains a profiler. Profiling shows you the bottlenecks in overview. [Citation]
  29. Document your code. [Citation]
  30. Learn the difference between good and bad code. [Citation]
  31. Stick to coding standards, it will make it easier for you to understand other people’s code and other people will be able to understand yours. [Citation]
  32. Separate code, content and presentation: keep your PHP code separate from your HTML. [Citation]
  33. Don’t bother using complex template systems such as Smarty, use the one that’s included in PHP already, see ob_get_contents and extract, and simply pull the data from your database. [Citation]
  34. Never trust variables coming from user land (such as from $_POST) use mysql_real_escape_string when using mysql, and htmlspecialchars when outputting as HTML. [Citation]
  35. For security reasons never have anything that could expose information about paths, extensions and configuration, such as display_errors or phpinfo() in your webroot. [Citation]
  36. Turn off register_globals (it’s disabled by default for a reason!). No script at production level should need this enabled as it is a security risk. Fix any scripts that require it on, and fix any scripts that require it off using unregister_globals(). Do this now, as it’s set to be removed in PHP6. [Citation]
  37. Avoid using plain text when storing and evaluating passwords to avoid exposure, instead use a hash, such as an md5 hash. [Citation]
  38. Use ip2long() and long2ip() to store IP addresses as integers instead of strings. [Citation]
  39. You can avoid reinventing the wheel by using the PEAR project, giving you existing code of a high standard. [Citation]
  40. When using header(’Location: ‘.$url); remember to follow it with a die(); as the script continues to run even though the location has changed or avoid using it all together where possible. [Citation]
  41. In OOP, if a method can be a static method, declare it static. Speed improvement is by a factor of 4. [Citation].
  42. Incrementing a local variable in an OOP method is the fastest. Nearly the same as calling a local variable in a function and incrementing a global variable is 2 times slow than a local variable. [Citation]
  43. Incrementing an object property (eg. $this->prop++) is 3 times slower than a local variable. [Citation]
  44. Incrementing an undefined local variable is 9-10 times slower than a pre-initialized one. [Citation]
  45. Just declaring a global variable without using it in a function slows things down (by about the same amount as incrementing a local var). PHP probably does a check to see if the global exists. [Citation]
  46. Method invocation appears to be independent of the number of methods defined in the class because I added 10 more methods to the test class (before and after the test method) with no change in performance. [Citation]
  47. Methods in derived classes run faster than ones defined in the base class. [Citation]
  48. A function call with one parameter and an empty function body takes about the same time as doing 7-8 $localvar++ operations. A similar method call is of course about 15 $localvar++ operations. [Citation]
  49. Not everything has to be OOP, often it is just overhead, each method and object call consumes a lot of memory. [Citation]
  50. Never trust user data, escape your strings that you use in SQL queries using mysql_real_escape_string, instead of mysql_escape_string or addslashes. Also note that if magic_quotes_gpc is enabled you should use stripslashes first. [Citation]
  51. Avoid the PHP mail() function header injection issue. [Citation]
  52. Unset your database variables (the password at a minimum), you shouldn’t need it after you make the database connection.
  53. RTFM! PHP offers a fantastic manual, possibly one of the best out there, which makes it a very hands on language, providing working examples and talking in plain English. Please USE IT! [Citation]

Also see:

มีแฟนเป็นโปรแกรมเมอร์ต้องทำใจ -_-‘

1. ไม่มีเวลาให้เหมือนคนทำงานอาชีพอื่นเขามีกัน – เพราะเขียนโปรแกรมไม่เสร็จซวย !!! งานใช้ logic สูงสมาธิต้องมากตาม แถมต้องเอาใจเจ้านาย (ลูกค้า) แก้งานก็ต้องแก้ บ่ายเบี่ยงไม่ได้ เดี่ยวไม่มีกิน T_T
2. พูดอะไรจะออกนอกโลกไปหมดแล้ว มนุษย์(แฟนเก่า) เขาฟังไม่รู้เรื่อง – เออ มีแต่คนบอกผมแบบนี้เหมือนกัน หลังๆ เลยต้องมานั่งนึกก่อนพูดเสมอ ไม่งั้นกลายเป็นศัพท์คอมฯ ออกมาหมด -_-‘
3. ทำงานจนเช้า ทำบ้าทำบออะไรนักหนา ไม่หลับไม่นอน – -‘ มันด่าได้ดี – ก็จริง นะเนี่ย T_T แต่กลับไปดูข้อหนึ่งซะ
4. เวลาเข้าร้านหนังสือ มันก็พุ่งไปแต่มุมคอมฯ ซื้อที ๆ นึง ปาไปเป็นพัน ๆ หนังสือจะท่วมห้องอยู่แล้ว อ่านหมดจริง ๆ รึงัย (ดูมันด่าสิ) – อ้าววว ไม่พัฒนาความรู้ก็โดยเด็กรุ่นหลังแซงหมดซิครับ โลกของโปรแกรมเมอร์มันไม่มี senior กับ junior นะ คุณช้าทุกอย่างก็ล้าสมัย

ตอนนี้หลังจากเป็นโสดมาได้เกือบ ๆ 2 เดือน มีความคิดว่าถ้ามีตอนนี้ ก็หาอาชีพเดียวกัน น่าจะจบ อย่างน้อย ๆ ก็คอเดียวกัน น่าจะเข้าใจว่ามันลำบากยังไงมั่ง นั่งทำงานหัวฟูเนี่ย

ไปต่อได้ที่ http://www.pantip.com/tech/developer/topic/DN1998879/DN1998879.html

PHPHoffman Framework Diagram

จาก entry ที่แล้ว ก็นั่งคิด ๆ ว่าตัว Framework จะมีส่วนหลัก ๆ อะไรบ้าง ตอนนี้ก็ได้ข้อสรุปในการสร้างออกมาแล้ว นั่งคิด และปรับว่าจะเอายังไงอยู่หลายวันอยู่ เพราะบางอย่างมันก็รักพี่เสียดายน้อง แต่หลัก ๆ คือใช้ PHP5.2 และใช้ Extension ต่าง ๆ ของ PHP หลายตัวซึ่งการที่ใช้ Extension ของ PHP เยอะ ด้วยเหตุที่เอา Zend Framework เข้ามาอยู่ใน Core ระบบ ทำให้ข้อกำหนดหลายอย่างต้องหนักไปทาง Zend หลายเ่รื่อง แต่ผลที่ได้รับคือตัว code ต่าง ๆ นั้นมีระเบียบและตามรูปแบบเพิ่มขึ้นเยอะ สวนตัว Extension Components ได้แนวคิดจาก Firefox ที่อยากได้อะไรเิพิ่มก็เอามาใส่นั้นแหละ นอกเหนือจากที่ตัว Zend และ Component หลัก แต่จะยังคงแนวคิดที่ว่าจำเป็นต้องใช้อะไรก็จะโหลดระหว่างตอนทำงานแค่นั้น ทำให้ตัว Controller ต้องไล่ทำใหม่ทำให้ ทุก ๆ action นั้นมีรายละเอียดในการตั้งค่าเพิ่มขึ้น แต่จะเห็นว่า Permission Control นั้นหายไป เพราะคิดไว้ว่าจะเป็นส่วนของ Routing แทน ตอนนี้ขอดูความเหมาะสมก่อน ส่วนที่ผู้เขียนนั้นต้องใส่ใจจะมีอยู่ในส่วนนอกของ Core ทั้งหมด

เหตุที่ใช้ Component ของ Zend Framework ก็จากเหตุผลเก่าจาก entry ที่แล้ว และเพราะมีระเบียบแบบแผนการพัฒนาในชุมชนที่แข็งมาก

ตอนนี้พยายามเคลียร์ ๆ งานที่จำประจำ แล้วลงมาทำส่วน Framework ให้มันออกมาเป็น public เสียที คนบ่นอยากใช้กันแย่แล้ว T_T

อ่อ ผมได้แนวคิดส่วน View จาก Sixhead Template ของคุณปีโป้ หลายส่วน ซึ่งตอนนี้เอาไว้ public plan ที่ 2 แล้วกันเพราะได้ดูแนวคิดแล้ว ช่วยทีมออกแบบกราฟฟิกได้เยอะ

อ่อ ตัว private version ที่เป็น alpha รุ่นแรก ถูกเขียนเป็น Shop Cart อยู่ที่ http://shop.thaiadmin.org แต่ผมมองว่ารุ่นนั้นก็ปล่อยได้แล้วแหละ แต่ว่ามองอีกมุม ยังยากอยู่อีก -_-‘ อยากให้มันง่ายกว่านี้ในการใช้งานเพิ่มขึ้น

แค่คิดก็นั่งลง coding มันแล้ว …….

[Update] – เหตุผลต่อมาในการใช้ PHP5 เพราะตัว MySQL Improved Extension นั้นทำงานได้ดีกว่ากับ MySQL ใน version ใหม่ ๆ ด้วย คือมีแล้วไม่ได้ใช้มันเสียดาย -_-‘

Redesign PHPHoffman Framework Architecture

เป็นปัญหาหนักอกอย่างรุนแรงที่จำเป็นต้อง redesign ตั้งแต่ยังไม่ออก beta การ redesign ในระดับ core นี่มันสุด ๆ จริง ๆ เพราะว่าอย่างที่หลาย ๆ entry ที่ผ่านมาเนี่ยกะว่าก็คงรู้ว่าผมกำลังทำ Framework ที่ออกไปทาง Conponent Base แทน และผสมตัว Automate เฉพาะที่จำเป็น ทำให้ไอ้ที่ทำ ๆ ตอนแรก ๆ ในแบบ Automate ทั้งหมดตั้งแต่แรก มันต้อง drop ไปหมดเลย เพราะเริ่มคิดว่า การทำเล็ก ๆ แล้วเอา plug-in มาใส่น่าจะตอบโจทย์มากกว่า เพราะอย่างแรกมันเป็นเรื่องของการโหลดตัวระบบที่มาก หรือน้อยขึ้นอยู่กับ component ที่ใช้ ไม่ใช่จะใช้แค่นี้แต่ดันเอาไส้ในออกมาเสียหมด มันเปลื้องเกินความจำเป็น แถมลอง monitor ตัว framework ที่เขียนในปัจจุบันนี่มันก็ใช้ memmory ไปพอสมควร แต่คิดว่ามันน่าจะน้อยกว่านี้ได้อีก แถมนี่จะเกือบปีแล้วยังไม่ได้ออก beta เลย แถมมีแต่คนถามว่าเมื่อไหร่จะให้โหลด ต้องบอกว่าตัวที่เป็น production ของ Framework ตัวนี้ก็อยู่ที่ shop.thaiadmin.org ครับ ส่วนตัวใหม่นี่จะเปลี่ยนภายในทั้งหมด พวก app ที่เขียน ๆ อยู่อาจต้อง recode ใหม่ด้วย แต่มันจำเป็นจริง ๆ แต่จะพยายาม backward ให้มากที่สุด

อ่อ ลืมบอกไปว่า PHPHoffman ตัวนี้ตามข้อกำหนดของ GoPHP5 ครับ ใครใช้ PHP 4 อดนะครับ (แล้ว Host ที่ใช้อยู่นี่จะเอาไงหล่ะเนี่ย -_-")