PHP Apachecon Dec.14, 2005. San Diego Rasmus Lerdorf <rasmus@php.net> http://talks.lerdorf.com/show/acon05
Slide 1/26 November 17 2005 Server-Side PHP is a Server-side language Even though it is embedded in HTML files much like the client-side Javascript language, PHP is server-side and all PHP tags will be replaced by the server before anything is sent to the web browser. � So if the HTML file contains: � <html> <?php echo "Hello World" ?> </html> What the end user would see with a "view source" in the browser would be: � <html> Hello World </html> - 2 -
Slide 2/26 November 17 2005 Idea This is a C program that generates an HTML page with a table populated from an SQL query. � #include <stdio.h> #include <stdlib.h> #include "mysql.h" MYSQL mysql; void err(void) { fprintf(stderr, "%s\n", mysql_error(&mysql) ); exit(1); } int gen_page() { puts("<html><body><table>"); if(!(mysql_connect(&mysql,"host","user","passwd"))) err(); if(mysql_select_db(&mysql,"my_db")) err(); if(mysql_query(&mysql,"SELECT * FROM my_table")) err(); if(!(res = mysql_store_result(&mysql))) err(); while((row = mysql_fetch_row(res))) { puts("<tr>"); for (i=0 ; i < mysql_num_fields(res); i++) printf("<td>%s</td>\n",row[i]); puts("</tr>"); } puts("</table></body></html>"); if (!mysql_eof(res)) err(); mysql_free_result(res); mysql_close(&mysql); } Here is the same thing in PHP which produces the same output. � <html><body><table> <?php mysql_connect('host','user','passwd') or die(mysql_error()); $res = mysql_db_query('my_db','SELECT * FROM my_table') or die(mysql_error()); while(($row = mysql_fetch_row($res))) { echo '<tr>'; for ($i=0 ; $i < mysql_num_fields($res); $i++) echo "<td>{$row[$i]}</td>\n"; echo '</tr>'; } ?> </table></body></html> - 3 -
Slide 3/26 November 17 2005 Fail Fast! Fail Fast � Fail Cheap � Be Lazy � The greatest inefficiencies come from solving problems you will never have. � - 4 -
Slide 4/26 November 17 2005 What is Large? o 75 properties � o 25 international portals � o 15 Languages � o Hundreds of millions of registered users � o Literally billions of page views per day � o Thousands of engineers spread around the world � Merger of 24 Web Companies � Make that 30 now with FareChase, MusicMatch, Flickr, DialPad, Konfabulator and Upcoming.org � - 5 -
Slide 5/26 November 17 2005 Why PHP? � o Easy to learn and manage - well-documented � o Works well on existing FreeBSD/Apache platform � o Easy to integrate existing C/C++ code � o Inherent HTML embedding � o Outperformed technologies it replaced � - 6 -
Slide 6/26 November 17 2005 Scale � Share-nothing Architecture o Like HTTP, each request is distinct � o Shared data is pushed down to the data-store layer � This gives us o Ability to load balance � o Invisible failover from one datacenter to another � o Better modularization of applications � o Easier to develop and debug � - 7 -
Slide 7/26 November 17 2005 Scale No Runtime Templating! o PHP is a templating system, we don't need another one � o Ok, sometimes you might, but don't do it at runtime � System Calls - PHP 5.1 Code Changes o No more than 1 stat() per PHP file per request � o Add a stat cache to PHP's expand_filepath code � o Don't stat if Apache has already stat'ed the file � o Get rid of excessive stats in the streams code � System Calls - Scripting Changes o Remove ./ from include_path and use relative path includes � o Use just a single base dir in include_path � o No open_basedir or safe_mode � - 8 -
Slide 8/26 November 17 2005 Scale Watch those compiler flags o Use non-PIC Apache DSO (gcc -prefer-non-pic) by using --without-pic � o Use platform-specific gcc flags � o ./configure --disable-all � Other Changes o Plenty of custom extensions and limit RINIT � o Filter all user data by default � o No $_COOKIE nor $_ENV for you and use JIT population for $_SERVER � o Careful use of the session extension � - 9 -
Slide 9/26 November 17 2005 Software Stack � o FreeBSD 4.x/6.x � o Apache 1.3.x � o Plenty of homegrown C/C++ code � o MySQL/Oracle � o PHP/Perl � o APC Opcode Cache � - 10 -
Slide 10/26 November 17 2005 People Architecture � - 11 -
Slide 11/26 November 17 2005 App Architecture A suggested architecture for a PHP application. The template layer should have as little business logic as possible. As you go down you have less presentation and more business logic. � - 12 -
Slide 12/26 November 17 2005 Large-Scale PHP Scaling PHP is all about scaling your backend � Or bigger � Debugging and Profiling, look at xdebug (xdebug.org) and apd (pecl/apd) � - 13 -
- 14 -
Slide 13/26 November 17 2005 PHP 5 PHP 5 � - 15 -
Slide 14/26 November 17 2005 XML All XML handling based on libxml2 <?php $dom = new domDocument; $dom->load('menu.xml'); ?> presentations/slides/intro/menu.xml <?xml version="1.0" encoding="ISO-8859-1"?>� <breakfast_menu>� <food itemno="1">� <name>Belgian Waffles</name>� <price>$5.95</price>� <description>two of our famous Belgian Waffles with � plenty of real maple syrup</description>� <calories>650</calories>� </food>� <food itemno="2">� <name>Strawberry Belgian Waffles</name>� <price>$7.95</price>� <description>light Belgian waffles covered with strawberries� and whipped cream</description>� <calories>900</calories>� </food>� <food itemno="3">� <name>Berry-Berry Belgian Waffles</name>� <price>$8.95</price>� <description>light Belgian waffles covered with an assortment of� fresh berries and whipped cream</description>� <calories>900</calories>� </food>� <food itemno="4">� <name>French Toast</name>� <price>$4.50</price>� <description>thick slices made from our homemade sourdough bread</description>� <calories>600</calories>� </food>� - 16 -
Recommend
More recommend