Presentation is loading. Please wait.

Presentation is loading. Please wait.

INFO 344 Web Tools And Development CK Wang University of Washington Spring 2014.

Similar presentations


Presentation on theme: "INFO 344 Web Tools And Development CK Wang University of Washington Spring 2014."— Presentation transcript:

1 INFO 344 Web Tools And Development CK Wang University of Washington Spring 2014

2 Announcements PA1 = do not write code to read csv. Just import it via phpMyAdmin (search google on how to do this) M 12:30pm to 1:20pm @ MGH 080 W 12:30pm to 1:20pm @ MGH 136 4/16 & 5/14 12:30pm to 1:20pm @ MGH 370H

3 Reminder! Teach you “How to Learn” I will purposely leave things out Search on Google! This will change your life. I promise.

4 Revisit Example of === – indexOfChar($string, $char) => look for char in string – returns 0 (index) or null (no char), we want to differentiate Example of == – $mySQL->execute() => returns null or FALSE if fails? Not sure, just do FALSE, cuz null also works – Convenient because HTML = string but for loop iteration = int, for example, if I have a year dropdown, and I press select and form submit, when the page refreshes I need to highlight the selected year, I have a for loop, 1900 to 2014, I can do == instead of === Generally use ===

5 PHP Best Practices

6 PHP files Always use not shortcuts (shortcuts might not be supported later) Leave out ?> if pure PHP file

7 Do not alternate outputs Output html either by typing html directly Or… echo “ …” Choose 1 or the other! Do not alternate unless in loops.

8 Use filter_var() Never trust user input filter_var('bob@example.com', FILTER_VALIDATE_EMAIL) Returns the filtered data, or FALSE if the filter fails List of filters at http://tw2.php.net/manual/en/filter.filters.php http://tw2.php.net/manual/en/filter.filters.php

9 Single Quotes for Array keys echo myArray[key];// avoid this echo myArray['key'];// do this! Double quotes = performance hit No quotes = works now but might not in the future – define('key', 'wrongkey'); – Or if PHP decides to stop supporting this

10 Error Reporting For Development error_reporting(E_ALL); For Production/Release error_reporting(0);

11 Run-time limit Default is 30s max_execution_time value defined in the php.ini Keep this unless you have a good reason… set_time_limit(int) => for selected scripts

12 PHP info phpinfo() is great for developer info Do NOT use phpinfo() in production!! Easy to hack your system if they know what modules/php version you have!!! PHP exploits for older versions are public! This also means… use the latest PHP version

13 Use XCache PHP code Bytecode Apache XCache!!! Big performance improvement! No need to compile for every request PHP code changes much less frequently Requests can be >1000/second

14 Tier Code Design

15 Coding is an Art In general, good code design is… – Easier to understand – Easier to make changes – Less prone to bugs

16 Front-end Development Already learnt this! Easier to find/read Easier to update/change This is huge… imagine if you had a bug that you spent 3 hours on and it’s because a style sheet is being added at a random place?

17 3-tier architecture Easier to find/read Easier to add/remove layers

18 Example Any thoughts?

19 Poor Design Database code Business/Logic Presentation ALL in One function!!

20 Problem Very hard to modify/change anything = cannot scale! Abstraction => more scalable

21 3-tier architecture DatabaseBusiness Presentation Easy to make changes!

22 For Example What modules do I need?

23 Code modules User Account database User Cart data (session) Book database (title, actors, etc) Review database Logic to determine % sale Presentation = for everything

24 Code modules Users database Cart session data Books database Reviews database Page Logic (ex: % off) Buy Page Layout/View

25 Group Discussion! Groups of 3, Pick your favorite website, Pick a page, Draw the code modules! Presentation! & Submit on Canvas!

26 General Coding Best Practices

27 DRY approach Don’t Repeat Yourself Minimize copy/paste, every copy/paste => potential headaches later when you update. Easy to miss one Use functions, variables to prevent repeating

28 CLEAN approach Make the code look CLEAN Indent properly, line things up properly, always use {} for if and while

29 CONSISTENT approach Be Consistent Naming convention for variables, functions, classes need to be meaningful and consistent

30 WYSIWYG approach What you see is what you get What the code does should be obvious w/o comments but add comments when necessary. Instead of complex code, simplify and make it clear & easier to understand

31 WHY? Why DRY, Clean, Consistent, WYSIWYG? It’s better! Easier to change, update, add to, etc. Why!!!!! Ok fine. at the very least, it makes it easier for someone else to inherit so you can get promoted to work on something else! HA!

32 Other best practices Never store clear passwords! Always store hashes of them. Privacy and security reasons. Learn OOP. It’s crucial in almost every code base you’ll ever work on. This class isn’t about OOP so ask Google if it isn’t already clear!

33 Final Note Why not use PHP frameworks? Zend? Cake? Learn low level fundamentals Understand how everything works Appreciate frameworks more when you do use it

34 Tutorials Learn PHP in 15 minutes http://www.youtube.com/watch?v=ZdP0KM49IVk

35 Resources http://net.tutsplus.com/tutorials/php/30-php-best-practices-for-beginners/ http://phpmentoring.org/ http://www.phptherightway.com/pages/The-Basics.html http://www.compileonline.com/execute_php_online.php http://net.tutsplus.com/tutorials/php/add-power-to-your-php-with-multi-tiered- applications/ http://net.tutsplus.com/tutorials/php/add-power-to-your-php-with-multi-tiered- applications/

36 Questions?


Download ppt "INFO 344 Web Tools And Development CK Wang University of Washington Spring 2014."

Similar presentations


Ads by Google