PHP for hacks                  Souri Datta      (sourind@yahoo-inc.com)
What is PHP?• Server side language• Very easy to learn• Available on LAMP stack (Linux Apache Mysql  PHP)• Does not requir...
What we need to learn (for hacks)?•   Enough PHP to handle simple request•   How to talk to backend data store using PHP• ...
Getting Started• You need a local server with PHP enabled.• XAMPP for windows and Mac OS• Linux has it by default
Getting Started       Create a file hello.php inside htdocs and open it in browserlike       this http://localhost/hello.p...
Basics• PHP blocks start with <?php and end with ?> -• Every line of PHP has to end with a semicolon  ";”• Variables in PH...
Mix Match            demo2.php
Displaying more complex data• You can define arrays in PHP using the array()  method    $lampstack = array(Linux,Apache,My...
Arrays         demo4.php
Arrayssizeof($array) - this will return the size of the array                                                          dem...
Associative Arrays<ul><?php$lampstack = array(  Operating System => Linux,  Server => Apache,  Database => MySQL,  Languag...
Functions<?phpfunction renderList($array){  if( sizeof($array) > 0 ){    echo <ul>;foreach( $array as $key => $item ){    ...
Interacting with the web - URL                        parameters<?php$name = Tom;// if there is no language defined, switc...
Loading content from the web<?php // define the URL to load $url = http://cricket.yahoo.com/player-profile/Sachin-Tendulka...
ParsingXML content• Demodemo9.php
ParsingJSON content• Demodemo9.php
Talking to Mysql db
Further Reference         http://www.php.net/     http://developer.yahoo.comhttp://www.slideshare.net/souridatta
Nods.js• A javascript runtime environment• Javascript is used to write client side code, but  with node.js, server side co...
Getting started• Download nods.js and install it  – http://nodejs.org/• You are ready to go!
Hello WorldCreate a file hello.jsFrom cmd line , run : node hello.jsOpen in browser : http://localhost:8888/
Advantages• Event-driven asynchronous i/o• Callbacks are attached to i/o  – Avoids blocking
Further reading• http://nodejs.org/• http://www.nodebeginner.org/• http://code.google.com/p/v8/
Thank you!
HackU PHP and Node.js
Upcoming SlideShare
Loading in...5
×

HackU PHP and Node.js

1,100

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,100
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

HackU PHP and Node.js

  1. 1. PHP for hacks Souri Datta (sourind@yahoo-inc.com)
  2. 2. What is PHP?• Server side language• Very easy to learn• Available on LAMP stack (Linux Apache Mysql PHP)• Does not require any special tools. Create a file with .php extension and you are done.
  3. 3. What we need to learn (for hacks)?• Enough PHP to handle simple request• How to talk to backend data store using PHP• How to parse XML/JSON in PHP• How to generate JSON in PHP
  4. 4. Getting Started• You need a local server with PHP enabled.• XAMPP for windows and Mac OS• Linux has it by default
  5. 5. Getting Started Create a file hello.php inside htdocs and open it in browserlike this http://localhost/hello.php <?php $school="iit-b"; echo "Hello, World $school"; ?>demo1.php
  6. 6. Basics• PHP blocks start with <?php and end with ?> -• Every line of PHP has to end with a semicolon ";”• Variables in PHP start with a $• You print out content to the document in PHP with the echo command.• $school is variable and it can be printed out• You can jump in and out of PHP anywhere in the document. So if you intersperse PHP with HTML blocks, that is totally fine. For example:
  7. 7. Mix Match demo2.php
  8. 8. Displaying more complex data• You can define arrays in PHP using the array() method $lampstack = array(Linux,Apache,MySQL,PHP);• If you simply want to display a complex datatype like this in PHP for debugging you can use the print_r() command $lampstack = array(Linux,Apache,MySQL,PHP);print_r($lampstack);
  9. 9. Arrays demo4.php
  10. 10. Arrayssizeof($array) - this will return the size of the array demo5.php
  11. 11. Associative Arrays<ul><?php$lampstack = array( Operating System => Linux, Server => Apache, Database => MySQL, Language => PHP);$length = sizeof($lampstack);$keys = array_keys($lampstack);for( $i = 0;$i < $length;$i++ ){ echo <li> . $keys[$i] . : . $lampstack[$keys[$i]] . </li>;}?></ul>
  12. 12. Functions<?phpfunction renderList($array){ if( sizeof($array) > 0 ){ echo <ul>;foreach( $array as $key => $item ){ echo <li> . $key . : . $item . </li>; } echo </ul>; }}$lampstack = array( Operating System => Linux, Server => Apache, Database => MySQL, Language => PHP);renderList($lampstack);?> demo6.php
  13. 13. Interacting with the web - URL parameters<?php$name = Tom;// if there is no language defined, switch to Englishif( !isset($_GET[language]) ){ $welcome = Oh, hello there, ;}if( $_GET[language] == hindi ){ $welcome = Namastae, ;}switch($_GET[font]){ case small: $size = 80; break; case medium: $size = 100; break; case large: $size = 120; break; default: $size = 100; break;}echo <style>body{font-size: . $size . %;}</style>;echo <h1>.$welcome.$name.</h1>;?> demo7.php
  14. 14. Loading content from the web<?php // define the URL to load $url = http://cricket.yahoo.com/player-profile/Sachin-Tendulkar_2962; // start cURL $ch = curl_init(); // tell cURL what the URL iscurl_setopt($ch, CURLOPT_URL, $url); // tell cURL that you want the data back from that URLcurl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // run cURL $output = curl_exec($ch); // end the cURL call (this also cleans up memory so it is // important)curl_close($ch); // display the output echo $output;?> demo8.php
  15. 15. ParsingXML content• Demodemo9.php
  16. 16. ParsingJSON content• Demodemo9.php
  17. 17. Talking to Mysql db
  18. 18. Further Reference http://www.php.net/ http://developer.yahoo.comhttp://www.slideshare.net/souridatta
  19. 19. Nods.js• A javascript runtime environment• Javascript is used to write client side code, but with node.js, server side code can be written• Runs over cmd line
  20. 20. Getting started• Download nods.js and install it – http://nodejs.org/• You are ready to go!
  21. 21. Hello WorldCreate a file hello.jsFrom cmd line , run : node hello.jsOpen in browser : http://localhost:8888/
  22. 22. Advantages• Event-driven asynchronous i/o• Callbacks are attached to i/o – Avoids blocking
  23. 23. Further reading• http://nodejs.org/• http://www.nodebeginner.org/• http://code.google.com/p/v8/
  24. 24. Thank you!
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×