SlideShare a Scribd company logo
1 of 19
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

What's hot (20)

07 Introduction to PHP #burningkeyboards
07 Introduction to PHP #burningkeyboards07 Introduction to PHP #burningkeyboards
07 Introduction to PHP #burningkeyboards
 
PHP Workshop Notes
PHP Workshop NotesPHP Workshop Notes
PHP Workshop Notes
 
Intro to php
Intro to phpIntro to php
Intro to php
 
Data Types In PHP
Data Types In PHPData Types In PHP
Data Types In PHP
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
PHP POWERPOINT SLIDES
PHP POWERPOINT SLIDESPHP POWERPOINT SLIDES
PHP POWERPOINT SLIDES
 
Intro to PHP
Intro to PHPIntro to PHP
Intro to PHP
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Loops PHP 04
Loops PHP 04Loops PHP 04
Loops PHP 04
 
Intermediate PHP
Intermediate PHPIntermediate PHP
Intermediate PHP
 
PHP - Introduction to PHP
PHP -  Introduction to PHPPHP -  Introduction to PHP
PHP - Introduction to PHP
 
PHP and MySQL
PHP and MySQLPHP and MySQL
PHP and MySQL
 
Introduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHPIntroduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHP
 
Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1
 
Introduction to php web programming - get and post
Introduction to php  web programming - get and postIntroduction to php  web programming - get and post
Introduction to php web programming - get and post
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
What Is Php
What Is PhpWhat Is Php
What Is Php
 
Basics PHP
Basics PHPBasics PHP
Basics PHP
 
PHP and MySQL PHP Written as a set of CGI binaries in C in ...
PHP and MySQL PHP Written as a set of CGI binaries in C in ...PHP and MySQL PHP Written as a set of CGI binaries in C in ...
PHP and MySQL PHP Written as a set of CGI binaries in C in ...
 
Open Source Package PHP & MySQL
Open Source Package PHP & MySQLOpen Source Package PHP & MySQL
Open Source Package PHP & MySQL
 

Viewers also liked

Ideation101 - Hacku ideas
Ideation101 - Hacku ideas Ideation101 - Hacku ideas
Ideation101 - Hacku ideas Sorabh Jain
 
Advanced Php/Mysql Training With live project
Advanced Php/Mysql Training With live projectAdvanced Php/Mysql Training With live project
Advanced Php/Mysql Training With live projectShaheel Khan
 
21146103 김남희
21146103 김남희21146103 김남희
21146103 김남희kimnamhee
 
HackU: How to think of a great idea
HackU: How to think of a great ideaHackU: How to think of a great idea
HackU: How to think of a great ideaTom Praison Praison
 
Backend accessible
Backend accessibleBackend accessible
Backend accessibleMark Casias
 
Tool Up Your LAMP Stack
Tool Up Your LAMP StackTool Up Your LAMP Stack
Tool Up Your LAMP StackLorna Mitchell
 

Viewers also liked (9)

Ideation101 - Hacku ideas
Ideation101 - Hacku ideas Ideation101 - Hacku ideas
Ideation101 - Hacku ideas
 
Advanced Php/Mysql Training With live project
Advanced Php/Mysql Training With live projectAdvanced Php/Mysql Training With live project
Advanced Php/Mysql Training With live project
 
Java performance
Java performanceJava performance
Java performance
 
21146103 김남희
21146103 김남희21146103 김남희
21146103 김남희
 
Hacking with Semantic Web
Hacking with Semantic WebHacking with Semantic Web
Hacking with Semantic Web
 
Planning LAMP infrastructure
Planning LAMP infrastructurePlanning LAMP infrastructure
Planning LAMP infrastructure
 
HackU: How to think of a great idea
HackU: How to think of a great ideaHackU: How to think of a great idea
HackU: How to think of a great idea
 
Backend accessible
Backend accessibleBackend accessible
Backend accessible
 
Tool Up Your LAMP Stack
Tool Up Your LAMP StackTool Up Your LAMP Stack
Tool Up Your LAMP Stack
 

Similar to PHP for hacks

sumana_PHP_mysql_IIT_BOMBAY_2013
sumana_PHP_mysql_IIT_BOMBAY_2013sumana_PHP_mysql_IIT_BOMBAY_2013
sumana_PHP_mysql_IIT_BOMBAY_2013Sumana Hariharan
 
PHP Basics and Demo HackU
PHP Basics and Demo HackUPHP Basics and Demo HackU
PHP Basics and Demo HackUAnshu Prateek
 
PHP: The easiest language to learn.
PHP: The easiest language to learn.PHP: The easiest language to learn.
PHP: The easiest language to learn.Binny V A
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHPprabhatjon
 
GettingStartedWithPHP
GettingStartedWithPHPGettingStartedWithPHP
GettingStartedWithPHPNat Weerawan
 
php fundamental
php fundamentalphp fundamental
php fundamentalzalatarunk
 
Php introduction with history of php
Php introduction with history of phpPhp introduction with history of php
Php introduction with history of phppooja bhandari
 
PHP from soup to nuts Course Deck
PHP from soup to nuts Course DeckPHP from soup to nuts Course Deck
PHP from soup to nuts Course DeckrICh morrow
 
PSGI and Plack from first principles
PSGI and Plack from first principlesPSGI and Plack from first principles
PSGI and Plack from first principlesPerl Careers
 
basic concept of php(Gunikhan sonowal)
basic concept of php(Gunikhan sonowal)basic concept of php(Gunikhan sonowal)
basic concept of php(Gunikhan sonowal)Guni Sonow
 
Php basic for vit university
Php basic for vit universityPhp basic for vit university
Php basic for vit universityMandakini Kumari
 
MIND sweeping introduction to PHP
MIND sweeping introduction to PHPMIND sweeping introduction to PHP
MIND sweeping introduction to PHPBUDNET
 

Similar to PHP for hacks (20)

sumana_PHP_mysql_IIT_BOMBAY_2013
sumana_PHP_mysql_IIT_BOMBAY_2013sumana_PHP_mysql_IIT_BOMBAY_2013
sumana_PHP_mysql_IIT_BOMBAY_2013
 
PHP Basics and Demo HackU
PHP Basics and Demo HackUPHP Basics and Demo HackU
PHP Basics and Demo HackU
 
PHP: The easiest language to learn.
PHP: The easiest language to learn.PHP: The easiest language to learn.
PHP: The easiest language to learn.
 
Starting Out With PHP
Starting Out With PHPStarting Out With PHP
Starting Out With PHP
 
Php with mysql ppt
Php with mysql pptPhp with mysql ppt
Php with mysql ppt
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
 
Modern php
Modern phpModern php
Modern php
 
WT_PHP_PART1.pdf
WT_PHP_PART1.pdfWT_PHP_PART1.pdf
WT_PHP_PART1.pdf
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
GettingStartedWithPHP
GettingStartedWithPHPGettingStartedWithPHP
GettingStartedWithPHP
 
php fundamental
php fundamentalphp fundamental
php fundamental
 
Php introduction with history of php
Php introduction with history of phpPhp introduction with history of php
Php introduction with history of php
 
php
phpphp
php
 
PHP from soup to nuts Course Deck
PHP from soup to nuts Course DeckPHP from soup to nuts Course Deck
PHP from soup to nuts Course Deck
 
PSGI and Plack from first principles
PSGI and Plack from first principlesPSGI and Plack from first principles
PSGI and Plack from first principles
 
Pecl Picks
Pecl PicksPecl Picks
Pecl Picks
 
basic concept of php(Gunikhan sonowal)
basic concept of php(Gunikhan sonowal)basic concept of php(Gunikhan sonowal)
basic concept of php(Gunikhan sonowal)
 
Php basic for vit university
Php basic for vit universityPhp basic for vit university
Php basic for vit university
 
MIND sweeping introduction to PHP
MIND sweeping introduction to PHPMIND sweeping introduction to PHP
MIND sweeping introduction to PHP
 
Php Mysql
Php Mysql Php Mysql
Php Mysql
 

Recently uploaded

SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 

Recently uploaded (20)

SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 

PHP for hacks

  • 1. PHP for hacks Tom Praison tpraison@yahoo-inc.com 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