SlideShare a Scribd company logo
Sumana H
(sumanah@yahoo-inc.com)
PHP for hacks
sumana_PHP_mysql_IIT_BOMBAY_2013
sumana_PHP_mysql_IIT_BOMBAY_2013
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.
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
Getting Started
• You need a local server with PHP enabled.
• XAMPP for windows
• MAMP for Mac OSx
• Linux has it by default
Create a file hello.php into htdocs and call it like this
http://localhost:80/hello.php
<?php
$college = “IIT-BOMBAY”;
echo “Hello “ . $college;
?>
Getting Started
• 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.
• $college 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:
<?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>
Mix Match
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
<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
<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
<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 = “Sumana”
// 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
demo9.php
Displaying JSON content
• Demo
demo9.php
Putting all together
Further Reference
http://www.php.net/
http://developer.yahoo.com
http://isithackday.com/hackday-
toolbox/phpforhacks/index.html
http://www.slideshare.net/sumanahariharan
https://github.com/sumanahariharan

More Related Content

PPTX
HackU PHP and Node.js
PPTX
PHP for hacks
PPTX
Phphacku iitd
PPT
Php mysql
KEY
Using PHP
PPTX
Introduction to php
PPT
Php, mysq lpart1
HackU PHP and Node.js
PHP for hacks
Phphacku iitd
Php mysql
Using PHP
Introduction to php
Php, mysq lpart1

What's hot (20)

PDF
Php, mysq lpart4(processing html form)
PPTX
Php basics
PPT
Class 6 - PHP Web Programming
PPT
Beginners PHP Tutorial
PDF
Php 5.5
PPT
Php Lecture Notes
PPTX
Loops PHP 04
DOC
Php tutorial
PDF
basic concept of php(Gunikhan sonowal)
PDF
Php a dynamic web scripting language
PPT
What Is Php
 
PPTX
Php
PPT
Chapter 02 php basic syntax
PPT
PHP and MySQL
PPT
Php(report)
PDF
Introduction to PHP
PPT
PHP Tutorials
PPT
Phpwebdevelping
PPT
PDF
Introduction to php web programming - get and post
Php, mysq lpart4(processing html form)
Php basics
Class 6 - PHP Web Programming
Beginners PHP Tutorial
Php 5.5
Php Lecture Notes
Loops PHP 04
Php tutorial
basic concept of php(Gunikhan sonowal)
Php a dynamic web scripting language
What Is Php
 
Php
Chapter 02 php basic syntax
PHP and MySQL
Php(report)
Introduction to PHP
PHP Tutorials
Phpwebdevelping
Introduction to php web programming - get and post
Ad

Similar to sumana_PHP_mysql_IIT_BOMBAY_2013 (20)

PPTX
Php hacku
PPTX
php basics
PPTX
PHP Basics and Demo HackU
PPTX
Upstate CSCI 450 PHP
PDF
WT_PHP_PART1.pdf
PDF
UNIT4.pdf php basic programming for begginers
PPTX
Php with mysql ppt
PPTX
Upstate CSCI 450 PHP Chapters 5, 12, 13
PPT
Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
PPT
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
PPT
Php i basic chapter 3
PPT
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
PPTX
Php unit i
PPTX
Unit 5-PHP Declaring variables, data types, array, string, operators, Expres...
PPTX
PPTX
PHP from soup to nuts Course Deck
ODP
PHP: The easiest language to learn.
PPT
Php introduction with history of php
PPT
php fundamental
PPT
php
Php hacku
php basics
PHP Basics and Demo HackU
Upstate CSCI 450 PHP
WT_PHP_PART1.pdf
UNIT4.pdf php basic programming for begginers
Php with mysql ppt
Upstate CSCI 450 PHP Chapters 5, 12, 13
Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php unit i
Unit 5-PHP Declaring variables, data types, array, string, operators, Expres...
PHP from soup to nuts Course Deck
PHP: The easiest language to learn.
Php introduction with history of php
php fundamental
php
Ad

Recently uploaded (20)

PDF
Booking.com The Global AI Sentiment Report 2025
PPTX
Sales & Distribution Management , LOGISTICS, Distribution, Sales Managers
PDF
How to Get Business Funding for Small Business Fast
PDF
ANALYZING THE OPPORTUNITIES OF DIGITAL MARKETING IN BANGLADESH TO PROVIDE AN ...
PDF
Charisse Litchman: A Maverick Making Neurological Care More Accessible
PPTX
Board-Reporting-Package-by-Umbrex-5-23-23.pptx
PDF
IFRS Notes in your pocket for study all the time
PDF
Outsourced Audit & Assurance in USA Why Globus Finanza is Your Trusted Choice
PPTX
operations management : demand supply ch
PPTX
Principles of Marketing, Industrial, Consumers,
PPTX
Slide gioi thieu VietinBank Quy 2 - 2025
PPTX
svnfcksanfskjcsnvvjknsnvsdscnsncxasxa saccacxsax
PDF
How to Get Approval for Business Funding
PDF
Module 3 - Functions of the Supervisor - Part 1 - Student Resource (1).pdf
PPTX
2025 Product Deck V1.0.pptxCATALOGTCLCIA
PDF
Deliverable file - Regulatory guideline analysis.pdf
PDF
Nante Industrial Plug Factory: Engineering Quality for Modern Power Applications
PDF
Keppel_Proposed Divestment of M1 Limited
PDF
Daniels 2024 Inclusive, Sustainable Development
PDF
Cours de Système d'information about ERP.pdf
Booking.com The Global AI Sentiment Report 2025
Sales & Distribution Management , LOGISTICS, Distribution, Sales Managers
How to Get Business Funding for Small Business Fast
ANALYZING THE OPPORTUNITIES OF DIGITAL MARKETING IN BANGLADESH TO PROVIDE AN ...
Charisse Litchman: A Maverick Making Neurological Care More Accessible
Board-Reporting-Package-by-Umbrex-5-23-23.pptx
IFRS Notes in your pocket for study all the time
Outsourced Audit & Assurance in USA Why Globus Finanza is Your Trusted Choice
operations management : demand supply ch
Principles of Marketing, Industrial, Consumers,
Slide gioi thieu VietinBank Quy 2 - 2025
svnfcksanfskjcsnvvjknsnvsdscnsncxasxa saccacxsax
How to Get Approval for Business Funding
Module 3 - Functions of the Supervisor - Part 1 - Student Resource (1).pdf
2025 Product Deck V1.0.pptxCATALOGTCLCIA
Deliverable file - Regulatory guideline analysis.pdf
Nante Industrial Plug Factory: Engineering Quality for Modern Power Applications
Keppel_Proposed Divestment of M1 Limited
Daniels 2024 Inclusive, Sustainable Development
Cours de Système d'information about ERP.pdf

sumana_PHP_mysql_IIT_BOMBAY_2013

  • 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.
  • 5. 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
  • 6. Getting Started • You need a local server with PHP enabled. • XAMPP for windows • MAMP for Mac OSx • Linux has it by default
  • 7. Create a file hello.php into htdocs and call it like this http://localhost:80/hello.php <?php $college = “IIT-BOMBAY”; echo “Hello “ . $college; ?> Getting Started
  • 8. • 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. • $college 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. <?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> Mix Match 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 <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 <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 <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 = “Sumana” // 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