HackU PHP and Node.js
-
Upload
souridatta -
Category
Technology
-
view
1.096 -
download
0
Transcript of HackU PHP and Node.js
![Page 2: HackU PHP and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022052321/55644440d8b42ad6268b46fc/html5/thumbnails/2.jpg)
![Page 3: HackU PHP and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022052321/55644440d8b42ad6268b46fc/html5/thumbnails/3.jpg)
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.
![Page 4: HackU PHP and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022052321/55644440d8b42ad6268b46fc/html5/thumbnails/4.jpg)
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
![Page 5: HackU PHP and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022052321/55644440d8b42ad6268b46fc/html5/thumbnails/5.jpg)
Getting Started• You need a local server with PHP enabled.• XAMPP for windows and Mac OS• Linux has it by default
![Page 6: HackU PHP and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022052321/55644440d8b42ad6268b46fc/html5/thumbnails/6.jpg)
<?php $school="iit-b"; echo "Hello, World $school";?>
Create a file hello.php inside htdocs and open it in browser like this http://localhost/hello.php
Getting Started
demo1.php
![Page 7: HackU PHP and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022052321/55644440d8b42ad6268b46fc/html5/thumbnails/7.jpg)
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:
![Page 8: HackU PHP and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022052321/55644440d8b42ad6268b46fc/html5/thumbnails/8.jpg)
Mix Match
demo2.php
![Page 9: HackU PHP and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022052321/55644440d8b42ad6268b46fc/html5/thumbnails/9.jpg)
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);
![Page 10: HackU PHP and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022052321/55644440d8b42ad6268b46fc/html5/thumbnails/10.jpg)
Arrays
demo4.php
![Page 11: HackU PHP and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022052321/55644440d8b42ad6268b46fc/html5/thumbnails/11.jpg)
Arrays
sizeof($array) - this will return the size of the array
demo5.php
![Page 12: HackU PHP and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022052321/55644440d8b42ad6268b46fc/html5/thumbnails/12.jpg)
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>
![Page 13: HackU PHP and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022052321/55644440d8b42ad6268b46fc/html5/thumbnails/13.jpg)
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
![Page 14: HackU PHP and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022052321/55644440d8b42ad6268b46fc/html5/thumbnails/14.jpg)
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
![Page 15: HackU PHP and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022052321/55644440d8b42ad6268b46fc/html5/thumbnails/15.jpg)
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 is curl_setopt($ch, CURLOPT_URL, $url); // tell cURL that you want the data back from that URL curl_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
![Page 16: HackU PHP and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022052321/55644440d8b42ad6268b46fc/html5/thumbnails/16.jpg)
Parsing XML content• Demo
demo9.php
![Page 17: HackU PHP and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022052321/55644440d8b42ad6268b46fc/html5/thumbnails/17.jpg)
Parsing JSON content• Demo
demo9.php
![Page 18: HackU PHP and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022052321/55644440d8b42ad6268b46fc/html5/thumbnails/18.jpg)
Talking to Mysql db
![Page 19: HackU PHP and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022052321/55644440d8b42ad6268b46fc/html5/thumbnails/19.jpg)
Further Referencehttp://www.php.net/
http://developer.yahoo.comhttp://www.slideshare.net/souridatta
![Page 20: HackU PHP and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022052321/55644440d8b42ad6268b46fc/html5/thumbnails/20.jpg)
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
![Page 21: HackU PHP and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022052321/55644440d8b42ad6268b46fc/html5/thumbnails/21.jpg)
Getting started• Download nods.js and install it– http://nodejs.org/
• You are ready to go!
![Page 22: HackU PHP and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022052321/55644440d8b42ad6268b46fc/html5/thumbnails/22.jpg)
Hello World
Create a file hello.js
From cmd line , run : node hello.js
Open in browser : http://localhost:8888/
![Page 23: HackU PHP and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022052321/55644440d8b42ad6268b46fc/html5/thumbnails/23.jpg)
Advantages• Event-driven asynchronous i/o
• Callbacks are attached to i/o– Avoids blocking
![Page 24: HackU PHP and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022052321/55644440d8b42ad6268b46fc/html5/thumbnails/24.jpg)
Further reading• http://nodejs.org/• http://www.nodebeginner.org/• http://code.google.com/p/v8/
![Page 25: HackU PHP and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022052321/55644440d8b42ad6268b46fc/html5/thumbnails/25.jpg)
Thank you!