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
Introduction to php
PPTX
HackU PHP and Node.js
PPTX
PHP for hacks
PPTX
Phphacku iitd
PPT
Php mysql
KEY
Using PHP
PPT
Php, mysq lpart1
Introduction to php
HackU PHP and Node.js
PHP for hacks
Phphacku iitd
Php mysql
Using 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
PPT
Chapter 02 php basic syntax
PPT
PHP and MySQL
PPT
Php(report)
PDF
Introduction to PHP
PPT
PHP Tutorials
PPT
Phpwebdevelping
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
 
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
PPT
PPTX
PHP Basics and Demo HackU
PPT
Php introduction
PPT
Php classes in mumbai
PPT
rtwerewr
PPT
PHP and MySQL with snapshots
PPT
PPT
PPT
Synapseindia reviews on array php
PPT
php fundamental
PPT
PPT
Php introduction with history of php
PPTX
Php by shivitomer
PPT
Php course-in-navimumbai
PPTX
Quick beginner to Lower-Advanced guide/tutorial in PHP
PPT
PHP - Introduction to PHP
PPT
introduction to php web programming 2024.ppt
PPT
Synapseindia reviews sharing intro on php
PPT
Synapseindia reviews sharing intro on php
PHP Basics and Demo HackU
Php introduction
Php classes in mumbai
rtwerewr
PHP and MySQL with snapshots
Synapseindia reviews on array php
php fundamental
Php introduction with history of php
Php by shivitomer
Php course-in-navimumbai
Quick beginner to Lower-Advanced guide/tutorial in PHP
PHP - Introduction to PHP
introduction to php web programming 2024.ppt
Synapseindia reviews sharing intro on php
Synapseindia reviews sharing intro on php
Ad

Recently uploaded (20)

PPT
Retail Management and Retail Markets and Concepts
PPTX
IndustrialAIGuerillaInnovatorsARCPodcastEp3.pptx
PDF
Engaging Stakeholders in Policy Discussions: A Legal Framework (www.kiu.ac.ug)
PPTX
Understanding Procurement Strategies.pptx Your score increases as you pick a ...
PDF
France's Top 5 Promising EdTech Companies to Watch in 2025.pdf
PDF
How to run a consulting project from scratch
PDF
Business Communication for MBA Students.
PDF
Consumer Behavior in the Digital Age (www.kiu.ac.ug)
PDF
109422672-Doc-8973-05-Security-Manual-Seventh-Edition.pdf
PDF
757557697-CERTIKIT-ISO22301-Implementation-Guide-v6.pdf
PPTX
IITM - FINAL Option - 01 - 12.08.25.pptx
PPTX
Supply Chain under WAR (Managing Supply Chain Amid Political Conflict).pptx
PDF
The Influence of Historical Figures on Legal Communication (www.kiu.ac.ug)
PDF
dataZense for Data Analytics unleashed features
PPTX
interschool scomp.pptxzdkjhdjvdjvdjdhjhieij
PDF
Cross-Cultural Leadership Practices in Education (www.kiu.ac.ug)
PDF
Highest-Paid CEO in 2025_ You Won’t Believe Who Tops the List.pdf
PDF
Communication Tactics in Legal Contexts: Historical Case Studies (www.kiu.ac...
PPT
BCG内部幻灯片撰写. slide template BCG.slide template
PDF
HQ #118 / 'Building Resilience While Climbing the Event Mountain
Retail Management and Retail Markets and Concepts
IndustrialAIGuerillaInnovatorsARCPodcastEp3.pptx
Engaging Stakeholders in Policy Discussions: A Legal Framework (www.kiu.ac.ug)
Understanding Procurement Strategies.pptx Your score increases as you pick a ...
France's Top 5 Promising EdTech Companies to Watch in 2025.pdf
How to run a consulting project from scratch
Business Communication for MBA Students.
Consumer Behavior in the Digital Age (www.kiu.ac.ug)
109422672-Doc-8973-05-Security-Manual-Seventh-Edition.pdf
757557697-CERTIKIT-ISO22301-Implementation-Guide-v6.pdf
IITM - FINAL Option - 01 - 12.08.25.pptx
Supply Chain under WAR (Managing Supply Chain Amid Political Conflict).pptx
The Influence of Historical Figures on Legal Communication (www.kiu.ac.ug)
dataZense for Data Analytics unleashed features
interschool scomp.pptxzdkjhdjvdjvdjdhjhieij
Cross-Cultural Leadership Practices in Education (www.kiu.ac.ug)
Highest-Paid CEO in 2025_ You Won’t Believe Who Tops the List.pdf
Communication Tactics in Legal Contexts: Historical Case Studies (www.kiu.ac...
BCG内部幻灯片撰写. slide template BCG.slide template
HQ #118 / 'Building Resilience While Climbing the Event Mountain

sumana_PHP_mysql_IIT_BOMBAY_2013