SlideShare a Scribd company logo
PHP for hacks
                  Souri Datta
      (sourind@yahoo-inc.com)
HackU PHP and Node.js
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.
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
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.php
                 <?php
                  $school="iit-b";
                  echo "Hello, World $school";
                 ?>




demo1.php
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:
Mix Match




            demo2.php
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);
Arrays




         demo4.php
Arrays




sizeof($array) - this will return the size of the array




                                                          demo5.php
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>
Functions
<?php
function 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
Interacting with the web - URL
                        parameters
<?php
$name = 'Tom';

// if there is no language defined, switch to English
if( !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
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
ParsingXML content
• Demo




demo9.php
ParsingJSON content
• Demo




demo9.php
Talking to Mysql db
Further Reference
         http://www.php.net/
     http://developer.yahoo.com
http://www.slideshare.net/souridatta
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
Getting started
• Download nods.js and install it
  – http://nodejs.org/
• You are ready to go!
Hello World

Create a file hello.js




From cmd line , run : node hello.js

Open 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!

More Related Content

PPTX
PHP for hacks
PPTX
PPTX
Phphacku iitd
PPT
Php mysql
KEY
Using PHP
PPTX
sumana_PHP_mysql_IIT_BOMBAY_2013
PPTX
PHP Basics and Demo HackU
PDF
basic concept of php(Gunikhan sonowal)
PHP for hacks
Phphacku iitd
Php mysql
Using PHP
sumana_PHP_mysql_IIT_BOMBAY_2013
PHP Basics and Demo HackU
basic concept of php(Gunikhan sonowal)

What's hot (20)

PPTX
Introduction to PHP
ODP
PHP: The easiest language to learn.
PPT
Php basic for vit university
PPT
Class 6 - PHP Web Programming
PPT
Php Lecture Notes
PDF
Introduction to PHP
PPTX
Loops PHP 04
PPT
PHP POWERPOINT SLIDES
PPT
Introduction To PHP
PPTX
Introduction to php
PPTX
Php with mysql ppt
ODP
PHP5.5 is Here
PPS
Php security3895
PDF
Introduction to PHP - Basics of PHP
PDF
07 Introduction to PHP #burningkeyboards
PPT
php $_GET / $_POST / $_SESSION
KEY
Intermediate PHP
PPT
What Is Php
 
PPT
Intro to php
Introduction to PHP
PHP: The easiest language to learn.
Php basic for vit university
Class 6 - PHP Web Programming
Php Lecture Notes
Introduction to PHP
Loops PHP 04
PHP POWERPOINT SLIDES
Introduction To PHP
Introduction to php
Php with mysql ppt
PHP5.5 is Here
Php security3895
Introduction to PHP - Basics of PHP
07 Introduction to PHP #burningkeyboards
php $_GET / $_POST / $_SESSION
Intermediate PHP
What Is Php
 
Intro to php

Similar to HackU PHP and Node.js (20)

PDF
Winter%200405%20-%20Beginning%20PHP
PDF
Winter%200405%20-%20Beginning%20PHP
PPTX
PHP Hypertext Preprocessor
PPT
Synapseindia reviews on array php
PPT
PPT
PHP - Introduction to PHP Fundamentals
PDF
Lecture8
PPTX
PHP ITCS 323
PPTX
PHP language presentation
PPTX
Server – side Technologies PHP for web dev.pptx
PPTX
Unit 5-PHP Declaring variables, data types, array, string, operators, Expres...
PPT
Php classes in mumbai
PPTX
Day1
PPT
PPT
Synapseindia reviews sharing intro on php
PPT
Synapseindia reviews sharing intro on php
PPT
rtwerewr
PPT
php 1
PDF
Phpbasics
PPTX
PHP from soup to nuts Course Deck
Winter%200405%20-%20Beginning%20PHP
Winter%200405%20-%20Beginning%20PHP
PHP Hypertext Preprocessor
Synapseindia reviews on array php
PHP - Introduction to PHP Fundamentals
Lecture8
PHP ITCS 323
PHP language presentation
Server – side Technologies PHP for web dev.pptx
Unit 5-PHP Declaring variables, data types, array, string, operators, Expres...
Php classes in mumbai
Day1
Synapseindia reviews sharing intro on php
Synapseindia reviews sharing intro on php
rtwerewr
php 1
Phpbasics
PHP from soup to nuts Course Deck

Recently uploaded (20)

PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Big Data Technologies - Introduction.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Machine learning based COVID-19 study performance prediction
PPT
Teaching material agriculture food technology
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Cloud computing and distributed systems.
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
cuic standard and advanced reporting.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Spectroscopy.pptx food analysis technology
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
MYSQL Presentation for SQL database connectivity
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
Encapsulation_ Review paper, used for researhc scholars
Big Data Technologies - Introduction.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Machine learning based COVID-19 study performance prediction
Teaching material agriculture food technology
Advanced methodologies resolving dimensionality complications for autism neur...
Cloud computing and distributed systems.
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
NewMind AI Weekly Chronicles - August'25 Week I
Reach Out and Touch Someone: Haptics and Empathic Computing
cuic standard and advanced reporting.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Spectroscopy.pptx food analysis technology
Unlocking AI with Model Context Protocol (MCP)
MYSQL Presentation for SQL database connectivity
The AUB Centre for AI in Media Proposal.docx
Chapter 3 Spatial Domain Image Processing.pdf
Per capita expenditure prediction using model stacking based on satellite ima...

HackU PHP and Node.js

  • 1. PHP for hacks Souri Datta (sourind@yahoo-inc.com)
  • 3. 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.
  • 4. 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
  • 5. Getting Started • You need a local server with PHP enabled. • XAMPP for windows and Mac OS • Linux has it by default
  • 6. 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
  • 7. 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:
  • 8. Mix Match demo2.php
  • 9. 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);
  • 10. Arrays demo4.php
  • 11. Arrays sizeof($array) - this will return the size of the array demo5.php
  • 12. 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>
  • 13. Functions <?php function 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
  • 14. Interacting with the web - URL parameters <?php $name = 'Tom'; // if there is no language defined, switch to English if( !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
  • 15. 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
  • 19. Further Reference http://www.php.net/ http://developer.yahoo.com http://www.slideshare.net/souridatta
  • 20. 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
  • 21. Getting started • Download nods.js and install it – http://nodejs.org/ • You are ready to go!
  • 22. Hello World Create a file hello.js From cmd line , run : node hello.js Open in browser : http://localhost:8888/
  • 23. Advantages • Event-driven asynchronous i/o • Callbacks are attached to i/o – Avoids blocking
  • 24. Further reading • http://nodejs.org/ • http://www.nodebeginner.org/ • http://code.google.com/p/v8/