lecture – 2 php introductionvip.gatech.edu/wiki/images/8/8a/lec2011_2.pdf · php introduction...

24
Lecture – 2 PHP Introduction Himani Manglani [email protected]

Upload: others

Post on 25-Sep-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture – 2 PHP Introductionvip.gatech.edu/wiki/images/8/8a/Lec2011_2.pdf · PHP Introduction Himani Manglani himanim@gatech.edu ... Linux (operating system), Apache HTTP Server,

Lecture – 2

PHP Introduction

Himani Manglani [email protected]

Page 2: Lecture – 2 PHP Introductionvip.gatech.edu/wiki/images/8/8a/Lec2011_2.pdf · PHP Introduction Himani Manglani himanim@gatech.edu ... Linux (operating system), Apache HTTP Server,

  LAMP is an acronym for a solution stack of free, open source software, originally coined from the first letters of Linux (operating system), Apache HTTP Server, MySQL (database software) and Perl/PHP/Python, principal components to build a viable general purpose web server.

Page 3: Lecture – 2 PHP Introductionvip.gatech.edu/wiki/images/8/8a/Lec2011_2.pdf · PHP Introduction Himani Manglani himanim@gatech.edu ... Linux (operating system), Apache HTTP Server,

  Unix clone

  Kernel of an Operating System

  Different distributions: Debian, Mandrake, RedHat and many others Totally free, but not "free" as in "free beer”

  Multi-user: Secure "by design“ Multi-platform: Runs on many different architectures: Intel, Motorola, Sparc, PowerPC A networking OS

Page 4: Lecture – 2 PHP Introductionvip.gatech.edu/wiki/images/8/8a/Lec2011_2.pdf · PHP Introduction Himani Manglani himanim@gatech.edu ... Linux (operating system), Apache HTTP Server,

  Flexible, Easy to configure

  Serves over 70% of all websites Robust, fast: It just works

  Multiple serving architectures: the best for each platform

  Many extensions

Page 5: Lecture – 2 PHP Introductionvip.gatech.edu/wiki/images/8/8a/Lec2011_2.pdf · PHP Introduction Himani Manglani himanim@gatech.edu ... Linux (operating system), Apache HTTP Server,

  Very easy to setup and use

  But still quite powerful

  Robust and fast: It can easily outperform e.g. Oracle in simple tasks

  Open Source, but also commercially backed   Cross-platform

Page 6: Lecture – 2 PHP Introductionvip.gatech.edu/wiki/images/8/8a/Lec2011_2.pdf · PHP Introduction Himani Manglani himanim@gatech.edu ... Linux (operating system), Apache HTTP Server,

  Web Scripting language by design

  Extremely shallow learning curve

  Powerful and flexible

  Cross-platform

  Easily extended

Page 7: Lecture – 2 PHP Introductionvip.gatech.edu/wiki/images/8/8a/Lec2011_2.pdf · PHP Introduction Himani Manglani himanim@gatech.edu ... Linux (operating system), Apache HTTP Server,

  Yahoo! (Apache and PHP)

  Military

  Lufthansa

…. and many more

Page 8: Lecture – 2 PHP Introductionvip.gatech.edu/wiki/images/8/8a/Lec2011_2.pdf · PHP Introduction Himani Manglani himanim@gatech.edu ... Linux (operating system), Apache HTTP Server,

PHP is a server side language. PHP has an HTML-centric approach.

Page 9: Lecture – 2 PHP Introductionvip.gatech.edu/wiki/images/8/8a/Lec2011_2.pdf · PHP Introduction Himani Manglani himanim@gatech.edu ... Linux (operating system), Apache HTTP Server,

HTML :

<html>

Hello World

</html>

PHP:

<html>

<?php echo "Hello World" ?>

</html>

Page 10: Lecture – 2 PHP Introductionvip.gatech.edu/wiki/images/8/8a/Lec2011_2.pdf · PHP Introduction Himani Manglani himanim@gatech.edu ... Linux (operating system), Apache HTTP Server,

  The 4 available tag styles

<html>

<body>

<? echo 'Short Tags - Most common' ?>

<?php echo 'Long Tags - Portable' ?>

<%= 'ASP Tags' %>

<script language="php"> 

echo 'Really Long Tags - rarely used';

</script>

</body>

</html>

Page 11: Lecture – 2 PHP Introductionvip.gatech.edu/wiki/images/8/8a/Lec2011_2.pdf · PHP Introduction Himani Manglani himanim@gatech.edu ... Linux (operating system), Apache HTTP Server,

<?php

$foo = 1;    

$bar = "Testing";    

$xyz = 3.14;    

$foo = $foo + 1;

?>

Page 12: Lecture – 2 PHP Introductionvip.gatech.edu/wiki/images/8/8a/Lec2011_2.pdf · PHP Introduction Himani Manglani himanim@gatech.edu ... Linux (operating system), Apache HTTP Server,

<?php    

$foo[1] = 1;      

$foo[2] = 2;    

$bar[1][2] = 3;

?>

Page 13: Lecture – 2 PHP Introductionvip.gatech.edu/wiki/images/8/8a/Lec2011_2.pdf · PHP Introduction Himani Manglani himanim@gatech.edu ... Linux (operating system), Apache HTTP Server,

<html>

<head>

<title>Example #1 TDavid's Very First PHP Script ever!</title>

</head>

<? print(Date("l F d, Y")); ?>

<body></body>

</html>

Page 14: Lecture – 2 PHP Introductionvip.gatech.edu/wiki/images/8/8a/Lec2011_2.pdf · PHP Introduction Himani Manglani himanim@gatech.edu ... Linux (operating system), Apache HTTP Server,

<?php    

phpinfo();    

foo();    

$len = strlen($foo);

?>

<?php

// Show all information, defaults to INFO_ALL

phpinfo();

// Show just the module information.

 phpinfo(8) //yields identical 

results.phpinfo(INFO_MODULES);?>

Page 15: Lecture – 2 PHP Introductionvip.gatech.edu/wiki/images/8/8a/Lec2011_2.pdf · PHP Introduction Himani Manglani himanim@gatech.edu ... Linux (operating system), Apache HTTP Server,

<?php    

while($foo) 

{        ...    }

?>

Page 16: Lecture – 2 PHP Introductionvip.gatech.edu/wiki/images/8/8a/Lec2011_2.pdf · PHP Introduction Himani Manglani himanim@gatech.edu ... Linux (operating system), Apache HTTP Server,

<?php    

echo $foo;    

printf(".2f",$price);?>

Page 17: Lecture – 2 PHP Introductionvip.gatech.edu/wiki/images/8/8a/Lec2011_2.pdf · PHP Introduction Himani Manglani himanim@gatech.edu ... Linux (operating system), Apache HTTP Server,

<? if(strstr($_SERVER['HTTP_USER_AGENT'],"MSIE")) { ?>

<b>

You are using Internet Explorer

</b>

<? } else { ?>

<b>You are not using Internet Explorer</b>

<? } ?>

Page 18: Lecture – 2 PHP Introductionvip.gatech.edu/wiki/images/8/8a/Lec2011_2.pdf · PHP Introduction Himani Manglani himanim@gatech.edu ... Linux (operating system), Apache HTTP Server,

<?php    

echo $foo;    

printf(".2f",$price);?>

Page 19: Lecture – 2 PHP Introductionvip.gatech.edu/wiki/images/8/8a/Lec2011_2.pdf · PHP Introduction Himani Manglani himanim@gatech.edu ... Linux (operating system), Apache HTTP Server,

Traditional

<form action="action.php" method="POST">

Your name: 

<input type=text name=name>

<br>You age: <input type=text name=age><br>

<input type=submit></form>

PHP

Hi <?php echo $_POST['name']?>.  You are 

<?php echo $_POST['age']?> years old.

Page 20: Lecture – 2 PHP Introductionvip.gatech.edu/wiki/images/8/8a/Lec2011_2.pdf · PHP Introduction Himani Manglani himanim@gatech.edu ... Linux (operating system), Apache HTTP Server,

Number

<?php   

 $a = 1234;   

 $b = 0777;    

$c = 0xff;    

$d = 1.25;    

echo "$a $b $c $d<br />\n";?>

Result: 1234 511 255 1.25

Page 21: Lecture – 2 PHP Introductionvip.gatech.edu/wiki/images/8/8a/Lec2011_2.pdf · PHP Introduction Himani Manglani himanim@gatech.edu ... Linux (operating system), Apache HTTP Server,

<?php    

$name = 'Rasmus 

$last'; // Single-quoted    

$str  = "Hi $name\n";   // Double-quoted    echo $str;?>

Output Hi Rasmus $last

Page 22: Lecture – 2 PHP Introductionvip.gatech.edu/wiki/images/8/8a/Lec2011_2.pdf · PHP Introduction Himani Manglani himanim@gatech.edu ... Linux (operating system), Apache HTTP Server,

<?php    

$greeting = true;   

 if($greeting) 

{      

echo "Hi Carl";        

$greeting = false;   

 }?>

Output : Hi Carl

Page 23: Lecture – 2 PHP Introductionvip.gatech.edu/wiki/images/8/8a/Lec2011_2.pdf · PHP Introduction Himani Manglani himanim@gatech.edu ... Linux (operating system), Apache HTTP Server,

Don't have to declare types

Automatic conversion done

<?php    echo 5 + "1.5" + "10e2";?>

Output : 1006.5

Page 24: Lecture – 2 PHP Introductionvip.gatech.edu/wiki/images/8/8a/Lec2011_2.pdf · PHP Introduction Himani Manglani himanim@gatech.edu ... Linux (operating system), Apache HTTP Server,

Questions?