SlideShare a Scribd company logo
PHP for hacks
                 Tom Praison
    tpraison@yahoo-inc.com
       IIT-Delhi 16-Aug-2012
Sample Codes
• The sample code is available for download
  https://github.com/tompraison/php_sample
What we need to learn?
•   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
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 your done.
How it works?
Getting Started
•   You need a local server with PHP enabled.
•   XAMPP for windows
•   MAMP for Mac OSx
•   Linux has it by default
Getting Started



Create a file hello.php into htdocs and call it like this
http://localhost:8888/hello.php
           <?php
            $school="iit-delhi";
            echo "Hello, World $school";
           ?>
Basic Syntax
• 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
• You can mix and match HTML and PHP
        <?php
         $origin = 'Outer Space';
         $planet = 'Earth';
         $plan = 9;
         $sceneryType = "awful";
        ?>
        <h1>Synopsis</h1><p>It was a peaceful time on
        planet <?php echo $planet;?> and people in the
        <?php echo $sceneryType;?> scenery were
        unaware of the diabolic plan <?php echo
        $plan;?> from <?php echo $origin;?> that will
        take their senses to the edge of what can be
        endured.</p>



                                                         demo1.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);




                                                        demo2.php
Arrays
• Accessing arrays using index

    <ul>
    <?php
    $lampstack = array('Linux','Apache','MySQL','PHP');
    echo '<li>Operating System:'.$lampstack[0] . '</li>';
    echo '<li>Server:' . $lampstack[1] . '</li>';
    echo '<li>Database:' . $lampstack[2] . '</li>';
    echo '<li>Language:' . $lampstack[3] . '</li>';
    ?>
    </ul>




                                                            demo3.php
Arrays
• Iterating through arrays

    <ul>
    <?php
    $lampstack = array('Linux','Apache','MySQL','PHP');
    $labels = array('Operating System','Server','Database','Language');
    $length = sizeof($lampstack);
    for( $i = 0;$i < $length;$i++ ){
      echo '<li>' . $labels[$i] . ':' . $lampstack[$i] . '</li>';
    }
    ?>
    </ul>
          sizeof($array) - this will return the size of the array




                                                                    demo4.php
Associative Arrays
• PHP has associative arrays with string keys
<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>


                                                                     demo5.php
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
Displaying XML content
• Demo – Showing Twitter trends given a place
    – Displaying XML Content
    – Displaying JSON




demo9.php
Connecting to MySQL
• Demo10.php from source code
• Simple example to fetch data from DB
Further Reference
             http://www.php.net/
         http://developer.yahoo.com
      http://isithackday.com/hackday-
        toolbox/phpforhacks/index.html
   http://www.slideshare.net/tompraison
https://github.com/tompraison/php_sample

More Related Content

PPTX
Phphacku iitd
Sorabh Jain
 
PPT
Php mysql
Manish Jain
 
KEY
Using PHP
Mark Casias
 
PPT
Class 6 - PHP Web Programming
Ahmed Swilam
 
PPTX
Php hacku
Tom Praison Praison
 
PPTX
HackU PHP and Node.js
souridatta
 
PPT
Php Lecture Notes
Santhiya Grace
 
Phphacku iitd
Sorabh Jain
 
Php mysql
Manish Jain
 
Using PHP
Mark Casias
 
Class 6 - PHP Web Programming
Ahmed Swilam
 
HackU PHP and Node.js
souridatta
 
Php Lecture Notes
Santhiya Grace
 

What's hot (20)

PDF
07 Introduction to PHP #burningkeyboards
Denis Ristic
 
PPT
PHP Workshop Notes
Pamela Fox
 
PPT
Intro to php
Sp Singh
 
PDF
Data Types In PHP
Mark Niebergall
 
PDF
Introduction to PHP
Bradley Holt
 
PPT
PHP POWERPOINT SLIDES
Ismail Mukiibi
 
PPT
Intro to PHP
Sandy Smith
 
PPT
Introduction to PHP
Jussi Pohjolainen
 
PPTX
Loops PHP 04
mohamedsaad24
 
KEY
Intermediate PHP
Bradley Holt
 
PPT
PHP - Introduction to PHP
Vibrant Technologies & Computers
 
PPT
PHP and MySQL
webhostingguy
 
PDF
Introduction to PHP - Basics of PHP
wahidullah mudaser
 
PDF
Web Development Course: PHP lecture 1
Gheyath M. Othman
 
PDF
Introduction to php web programming - get and post
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Introduction to PHP
Collaboration Technologies
 
PPT
What Is Php
AVC
 
PPT
PHP and MySQL PHP Written as a set of CGI binaries in C in ...
webhostingguy
 
PPT
Open Source Package PHP & MySQL
kalaisai
 
07 Introduction to PHP #burningkeyboards
Denis Ristic
 
PHP Workshop Notes
Pamela Fox
 
Intro to php
Sp Singh
 
Data Types In PHP
Mark Niebergall
 
Introduction to PHP
Bradley Holt
 
PHP POWERPOINT SLIDES
Ismail Mukiibi
 
Intro to PHP
Sandy Smith
 
Introduction to PHP
Jussi Pohjolainen
 
Loops PHP 04
mohamedsaad24
 
Intermediate PHP
Bradley Holt
 
PHP - Introduction to PHP
Vibrant Technologies & Computers
 
PHP and MySQL
webhostingguy
 
Introduction to PHP - Basics of PHP
wahidullah mudaser
 
Web Development Course: PHP lecture 1
Gheyath M. Othman
 
Introduction to php web programming - get and post
baabtra.com - No. 1 supplier of quality freshers
 
Introduction to PHP
Collaboration Technologies
 
What Is Php
AVC
 
PHP and MySQL PHP Written as a set of CGI binaries in C in ...
webhostingguy
 
Open Source Package PHP & MySQL
kalaisai
 

Viewers also liked (9)

PPTX
Ideation101 - Hacku ideas
Sorabh Jain
 
PDF
Advanced Php/Mysql Training With live project
Shaheel Khan
 
PPTX
Java performance
Sachin Shukla
 
PPTX
21146103 김남희
kimnamhee
 
PPTX
Hacking with Semantic Web
Tom Praison Praison
 
PDF
Planning LAMP infrastructure
David Timothy Strauss
 
PPTX
HackU: How to think of a great idea
Tom Praison Praison
 
PPTX
Backend accessible
Mark Casias
 
PDF
Tool Up Your LAMP Stack
Lorna Mitchell
 
Ideation101 - Hacku ideas
Sorabh Jain
 
Advanced Php/Mysql Training With live project
Shaheel Khan
 
Java performance
Sachin Shukla
 
21146103 김남희
kimnamhee
 
Hacking with Semantic Web
Tom Praison Praison
 
Planning LAMP infrastructure
David Timothy Strauss
 
HackU: How to think of a great idea
Tom Praison Praison
 
Backend accessible
Mark Casias
 
Tool Up Your LAMP Stack
Lorna Mitchell
 

Similar to PHP for hacks (20)

PPTX
sumana_PHP_mysql_IIT_BOMBAY_2013
Sumana Hariharan
 
PPTX
PHP Basics and Demo HackU
Anshu Prateek
 
PDF
Winter%200405%20-%20Beginning%20PHP
tutorialsruby
 
PDF
Winter%200405%20-%20Beginning%20PHP
tutorialsruby
 
PPT
Php introduction
Osama Ghandour Geris
 
PDF
PHP Programming: Intro
Things Lab
 
PDF
Phpbasics
PrinceGuru MS
 
PPT
PHP
sometech
 
PPT
05php
anshkhurana01
 
PPT
Php classes in mumbai
Vibrant Technologies & Computers
 
PPT
MIND sweeping introduction to PHP
BUDNET
 
PPT
rtwerewr
esolinhighered
 
PPTX
Quick beginner to Lower-Advanced guide/tutorial in PHP
Sanju Sony Kurian
 
PPT
php 1
tumetr1
 
PPT
05php
Shahid Usman
 
PPTX
Day1
IRWAA LLC
 
PPTX
PHP Hypertext Preprocessor
adeel990
 
PPT
Php introduction with history of php
pooja bhandari
 
PPT
php
Ramki Kv
 
PPT
php fundamental
zalatarunk
 
sumana_PHP_mysql_IIT_BOMBAY_2013
Sumana Hariharan
 
PHP Basics and Demo HackU
Anshu Prateek
 
Winter%200405%20-%20Beginning%20PHP
tutorialsruby
 
Winter%200405%20-%20Beginning%20PHP
tutorialsruby
 
Php introduction
Osama Ghandour Geris
 
PHP Programming: Intro
Things Lab
 
Phpbasics
PrinceGuru MS
 
Php classes in mumbai
Vibrant Technologies & Computers
 
MIND sweeping introduction to PHP
BUDNET
 
rtwerewr
esolinhighered
 
Quick beginner to Lower-Advanced guide/tutorial in PHP
Sanju Sony Kurian
 
php 1
tumetr1
 
Day1
IRWAA LLC
 
PHP Hypertext Preprocessor
adeel990
 
Php introduction with history of php
pooja bhandari
 
php fundamental
zalatarunk
 

Recently uploaded (20)

PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PPTX
Stamford - Community User Group Leaders_ Agentblazer Status, AI Sustainabilit...
Amol Dixit
 
PDF
Software Development Company | KodekX
KodekX
 
PPTX
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
Software Development Methodologies in 2025
KodekX
 
PPT
L2 Rules of Netiquette in Empowerment technology
Archibal2
 
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Chapter 1 Introduction to CV and IP Lecture Note.pdf
Getnet Tigabie Askale -(GM)
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PDF
REPORT: Heating appliances market in Poland 2024
SPIUG
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
PDF
Doc9.....................................
SofiaCollazos
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
Stamford - Community User Group Leaders_ Agentblazer Status, AI Sustainabilit...
Amol Dixit
 
Software Development Company | KodekX
KodekX
 
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
Software Development Methodologies in 2025
KodekX
 
L2 Rules of Netiquette in Empowerment technology
Archibal2
 
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Chapter 1 Introduction to CV and IP Lecture Note.pdf
Getnet Tigabie Askale -(GM)
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
REPORT: Heating appliances market in Poland 2024
SPIUG
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
Doc9.....................................
SofiaCollazos
 

PHP for hacks

  • 1. PHP for hacks Tom Praison [email protected] IIT-Delhi 16-Aug-2012
  • 2. Sample Codes • The sample code is available for download https://github.com/tompraison/php_sample
  • 3. What we need to learn? • 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. 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 your done.
  • 6. Getting Started • You need a local server with PHP enabled. • XAMPP for windows • MAMP for Mac OSx • Linux has it by default
  • 7. Getting Started Create a file hello.php into htdocs and call it like this http://localhost:8888/hello.php <?php $school="iit-delhi"; echo "Hello, World $school"; ?>
  • 8. Basic Syntax • 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:
  • 9. Mix Match • You can mix and match HTML and PHP <?php $origin = 'Outer Space'; $planet = 'Earth'; $plan = 9; $sceneryType = "awful"; ?> <h1>Synopsis</h1><p>It was a peaceful time on planet <?php echo $planet;?> and people in the <?php echo $sceneryType;?> scenery were unaware of the diabolic plan <?php echo $plan;?> from <?php echo $origin;?> that will take their senses to the edge of what can be endured.</p> demo1.php
  • 10. 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); demo2.php
  • 11. Arrays • Accessing arrays using index <ul> <?php $lampstack = array('Linux','Apache','MySQL','PHP'); echo '<li>Operating System:'.$lampstack[0] . '</li>'; echo '<li>Server:' . $lampstack[1] . '</li>'; echo '<li>Database:' . $lampstack[2] . '</li>'; echo '<li>Language:' . $lampstack[3] . '</li>'; ?> </ul> demo3.php
  • 12. Arrays • Iterating through arrays <ul> <?php $lampstack = array('Linux','Apache','MySQL','PHP'); $labels = array('Operating System','Server','Database','Language'); $length = sizeof($lampstack); for( $i = 0;$i < $length;$i++ ){ echo '<li>' . $labels[$i] . ':' . $lampstack[$i] . '</li>'; } ?> </ul> sizeof($array) - this will return the size of the array demo4.php
  • 13. Associative Arrays • PHP has associative arrays with string keys <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> demo5.php
  • 14. 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
  • 15. 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
  • 16. 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
  • 17. Displaying XML content • Demo – Showing Twitter trends given a place – Displaying XML Content – Displaying JSON demo9.php
  • 18. Connecting to MySQL • Demo10.php from source code • Simple example to fetch data from DB
  • 19. Further Reference http://www.php.net/ http://developer.yahoo.com http://isithackday.com/hackday- toolbox/phpforhacks/index.html http://www.slideshare.net/tompraison https://github.com/tompraison/php_sample